prototype 属性使您有能力向对象添加属性和方法。javascript
在本例中,将展现如何使用 prototype 属性来向对象添加属性:html
<script type="text/javascript"> function employee(name,job,born) { this.name=name; this.job=job; this.born=born; } var bill=new employee("Bill Gates","Engineer",1985); employee.prototype.salary=null; bill.salary=20000; document.write(bill.salary); </script>
输出:java
20000
一般来讲,javascript中的对象就是一个指向prototype的指针和一个自身的属性列表。javascript建立对象时采用了写时复制的理念。app
只有构造器才具备prototype属性,原型链继承就是建立一个新的指针,指向构造器的prototype属性。函数
prototype属性之因此特别,是由于javascript时读取属性时的遍历机制决定的。本质上它就是一个普通的指针。this
构造器包括:spa
1.Object 2.Function 3.Array 4.Date 5.String
下面咱们来举一些例子吧prototype
//每一个function都有一个默认的属性prototype,而这个prototype的constructor默认指向这个函数 //注意Person.constructor 不等于 Person.prototype.constructor. Function实例自带constructor属性 function Person(name) { this.name = name; }; Person.prototype.getName = function() { return this.name; }; var p = new Person("ZhangSan"); console.log(Person.prototype.constructor === Person); // true console.log(p.constructor === Person); // true ,这是由于p自己不包含constructor属性,因此这里其实调用的是Person.prototype.constructor
咱们的目的是要表示指针
1.代表Person继承自Animalcode
2. 代表p2是Person的实例
咱们修改一下prototype属性的指向,让Person能获取Animal中的prototype属性中的方法。也就是Person继承自Animal(人是野兽)
function Person(name) { this.name = name; }; Person.prototype.getName = function() { return this.name; }; var p1 = new Person("ZhangSan"); console.log(p.constructor === Person); // true console.log(Person.prototype.constructor === Person); // true function Animal(){ } Person.prototype = new Animal();//之因此不采用Person.prototype = Animal.prototype,是由于new 还有其余功能,最后总结。 var p2 = new Person("ZhangSan"); //(p2 -> Person.prototype -> Animal.prototype, 因此p2.constructor其实就是Animal.prototype.constructor) console.log(p2.constructor === Person); // 输出为false ,但咱们的本意是要这里为true的,代表p2是Person的实例。此时目的1达到了,目的2没达到。
但若是咱们这么修正
Person.prototype = new Animal(); Person.prototype.constructor = Person;
这时p2.consturctor是对了,指向的是Person,表示p2是Person类的实例,可是新问题出现了。此时目的2达到了,目的1没达到。
目的1和目的2此时互相矛盾,是由于此时prototype表达了矛盾的两个意思,
1表示父类是谁
2做为本身实例的原型来复制
所以咱们不能直接使用prototype属性来表示父类是谁,而是用getPrototypeOf()方法来知道父类是谁。
Person.prototype = new Animal(); Person.prototype.constructor = Person; var p2 = new Person("ZhangSan"); p2.constructor //显示 function Person() {} Object.getPrototypeOf(Person.prototype).constructor //显示 function Animal() {}
就把这两个概念给分开了 ,其实经过使用 hasOwnProperty()方法,何时访问的是实例属性,何时访问的是原型属性就一清二楚了。
new作了哪些事情?
当代码var p = new Person()执行时,new 作了以下几件事情:
建立一个空白对象
建立一个指向Person.prototype的指针
将这个对象经过this关键字传递到构造函数中并执行构造函数。
具体点来讲,在下面这段代码中,
Person.prototype.getName = function() { }
若是咱们经过
var person = new Person(); 其实相似于 var person = new Object(); person.getName = Person.prototype.getName;
所以经过person.getName()调用方法时,this指向的是这个新建立的对象,而不是prototype对象。
这其实在给现有函数加上新功能的状况下会用到,咱们能够这么扩展示有的方法:
//function myFunc 的写法基本上等于 var myFunc = new Function(); function myFunc () { } myFunc = function(func){ //能够在这里作点其余事情 return function(){ //能够在这里作点其余事情 return func.apply(this,arguments); } }(myFunc)
也能够在Function.prototype方法里直接经过this来访问上面代码的myFunc所指向的对象
function myFunc () { } if (!Function.prototype.extend) { Function.prototype.extend = function(){ var func = this; return function(){ func.apply(this,arguments); } } }; var myFunc = myFunc.extend();
最后总结一下:
若是采用Person.prototype = Animal.prototype来表示Person继承自Animal, instanceof方法也一样会显示p也是Animal的实例,返回为true.
之因此不采用此方法,是由于下面两个缘由:
1.new 建立了一个新对象,这样就避免了设置Person.prototype.constructor = Person 的时候也会致使Animal.prototype.constructor的值变为Person,而是动态给这个新建立的对象一个constructor实例属性。这样实例上的属性constructor就覆盖了Animal.prototype.constructor,这样Person.prototype.constructor和Animal.prototype.contructor就分开了。
2.Animal自身的this对象的属性没办法传递给Person
可是像下面这样直接调用构造函数又可能失败,或者产生其余影响。
Person.prototype = new Animal();
为了不这种状况,因此咱们引入了一个中间函数。因此正确的作法应该是
Person.prototype = (funtion(){ function F(){}; F.prototype = Animal.prototype return new F(); })()