javascript(5)原型链(Evernote)

1.简单的原型链:
var song= function(){
this.a =5;
this.b=6;
}
//function song (){
    this.a=5;
}
song.prototype={
add:function(a,b){return a+b+this.a+this.b;},
jian:function(a,b){return b-a;}
}
var jum= function(){
this.name="WTF";
}
jum.prototype=new song();
//让jum类继承song的一个实例(包括属性和方法) 若只想song的方法,保持其属性的私有性质,就能够设置 jum.prototype =song.prototype;

jum.prototype.constructor=jum; //若是没有将指向自己,则prototype.constructor就指向了一个空对象
调用方法:
var songjum= new jum();
songjum.add(3,4);
songjum.jian(2,5);
 
2.属性查找:属性在查找的时候是先查找自身的属性,若是没有再查找原型,再没有,再往上走,一直插到Object的原型上。
 
3.hasOwnProperty
var foo = {    hasOwnProperty: function() {        return false;    },    bar: 'Here be dragons'};foo.hasOwnProperty('bar'); // 老是返回 false// 使用{}对象的 hasOwnProperty,并将其上下为设置为foo{}.hasOwnProperty.call(foo, 'bar'); // true
相关文章
相关标签/搜索