详解prototype与__proto__

http://louiszhai.github.io/2015/12/17/prototype/

 

  1. prototype 是函数(function) 的一个属性, 它指向函数的原型.
  2. __proto__ 是对象的内部属性, 它指向构造器的原型, 对象依赖它进行原型链查询.

由上, prototype 只有函数才有, 其他(非函数)对象不具有该属性. 而 __proto__ 是对象的内部属性, 任何对象都拥有该属性.

由于对象构造器 (Object) 也是构造器, 构造器都是函数, 函数也是对象.

故, 对象构造器 (Object) 拥有3种身份:

  • 构造器
  • 函数
  • 对象

推而广之, 所有构造器都拥有上述3种身份.

 

源码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<script>
function employee(name,jobtitle,born){
    this.name=name;
    this.jobtitle=jobtitle;
    this.born=born;
    this.test = function(){
    console.log("test...");}
}
var fred=new employee("Fred Flintstone","Caveman",1970);
employee.prototype.salary=null;
fred.salary=20000;
document.write(fred.salary);
</script>

</body>
</html>

 

posted @ 2017-10-19 14:08  董永辉Bruno  阅读(115)  评论(0)    收藏  举报