关于prototype

之前听过课,可是这一块没怎么听懂,最近练了两个例子,又问了问小石同学,朦朦胧胧,感觉还是不太懂,记录点心得

最基本的例子

function Box(name,age){
    this.name=name;
    this.age=age;
    this.run=function(){
        return this.age+this.name;
    }
}
var box=new Box('lee',28);
console.log(box.run());

这个是传了两个参数,如果不传参的话,当然也行

function Box(){
    this.name='age';
    this.age='28';
    this.run=function(){
        return this.age+this.name;
    }
}
var box=new Box();
console.log(box.run());

如果用prototype的话,应该这样写

function Box(){
    this.name='age';
    this.age='28';
    }
    Box.prototype.run=function(){
        return this.age+this.name;
    }
var box=new Box();
console.log(box.run());

也可以写成这样

function Box(){
    this.name='age';
    this.age='28';
    }
    Box.prototype={
     run:function(){
        return this.age+this.name;}
    }
var box=new Box();
console.log(box.run());

然后解决了我一个问题,以前我是这样写的,报错了

function box1(){
    var sum=0;
    this.box2();
}
box1.prototype.box2=function(){
    var count=this.sum+1;
    console.log(count);
}

new box1()

其实,应该写成这样,不能用var,用var就变成了私有变量了

function box1(){
    this.sum=0;
    this.box2();
}
box1.prototype.box2=function(){
    var count=this.sum+1;
    console.log(count);
}

new box1()

还有就是new出来是一个对象,必须用对象.XXXX才行

function box1(){
this.sum=0;
}
box1.prototype.box2=function(){
var count=this.sum+1;
return count;
}
console.log(new box1().box2())

 如果是实例化方法和属性的话,他们的引用地址是不一样的

function box(name,age){
    this.name=name;        //实例化属性
    this.age=age;
    this.run=function(){            //实例化方法
        return this.name+this.age+'运行中';
    }
}
var box1=new box("lee",100);  //实例化后地址为1
var box2=new box("lee",100);  //实例化后地址为2
alert(box1.name==box2.name)   //true
alert(box1.run()==box2.run());   //true
alert(box1.run==box2.run);    //false 比较的是引用地址,方法的引用地址

如果是原型的属性和方法的话,他们的引用地址是一样的

function box(name,age){
    this.name=name;
    this.age=age;
}
box.prototype.run=function(){      //原型方法
    return this.name+this.age+'运行中';
}
var box1=new box("lee",100);  //原型方法,他们的地址是共享的
var box2=new box("lee",100);  //原型方法,他们的地址是共享的

alert(box1.run==box2.run);    //true 比较的是引用地址

根据上图,创建的每一个函数都有prototype属性,__proto__是一个指针,指向prototype原型对象,new出来的对象都有这个属性,他的作用是指向构造函数的原型属性constructor,通过这两个属性,就可以访问到原型里的属性和方法了。

function box(name,age){
    this.name=name;
    this.age=age;
}
box.prototype.run=function(){
    return this.name+this.age+'运行中';
}
var box1=new box("lee",100);
box.prototype      //box {}
box.prototype.constructor  //function box(name,age){....}  指向本身
box1.constructor    //function box(name,age){....}  指向本身
box1.constructor==box.prototype.constructor   //true
box1.__proto__     //box {}
box1.__proto__==box.prototype   //true

三、prototype字面量方法

function box(name,age){
    this.name=name;
    this.age=age;
}
box.prototype={
    constructor:box,      //强制指回box,如果不重新定义此属性,contructor指向了object,认为{}定义了一个数组
    run:function(){
    return this.name+this.age+'运行中';
    }
}
var box1=new box("lee",100);
alert(box1.constructor)

@

原文地址:https://www.cnblogs.com/change-oneself/p/4807236.html