prototype constructor _proto_释疑

prototype constructor __proto__释疑

function func(){}
typeof func.prototype//output : "object"; 注意这里的"object"的"o"是小写的
alert(typeof Object);//output: "function"
alert(typeof Number);//output: "function"
alert(typeof Function);//output: "function"

prototype属性通常为"object"类型,除了Function.prototype,它为"function"类型
任何函数对象都有prototype属性,除了Function.prototype,虽然Function.prototype是一个函数对象,但Function.prototype.prototype为空函数;相反,普通对象没有prototype属性
对象都有__proto__属性
Object.prototype.__proto__ = null;

alert(typeof anim.__proto__);//output: "object"
alert(typeof null);//output: "object"

注意Person.constructor 不等于 Person.prototype.constructor
 function Person(name) {
        this.name = name;
    };
    Person.prototype.getName = function() {
        return this.name;
    };
   
    /*以下语句的实际的运行操作是:
        第一步是建立一个新对象(叫p吧);
        第二步将该对象(p)内置的原型对象(即__proto__属性指向的对象)设置为构造函数(就是Person)prototype 属性引用的那个原型对象;
        第三步就是将该对象(p)作为this 参数调用构造函数(就是Person),完成成员设置等初始化工作。   
    */
    var p = new Person("ZhangSan");
    //若以以下两句代替上面那句,则不能执行p.getName()
    //var p = {};
    //Person.call(p);

    console.log(Person.prototype.constructor === Person); // true
    console.log(p.constructor === Person);  // true ,这是因为p本身不包含constructor属性,但p的__proto__指向Person.prototype,所以这里其实调用的是Person.prototype.constructor    
   
    ------------------------------------
    1.    function A(){
            var s = "mianqiang";
        }
        function B(){
       
        }
        var a = new A();
        A.prototype.constructor = B;
        alert(a.constructor);//输出的是B函数
       
    2.    function A(){
            var s = "mianqiang";
        }
        function B(){
       
        }
        var a = new A();
        a.constructor = B;//这种情况下constructor只是作为a的一个普通属性值
        alert(a.constructor);//输出的是B函数
        alert(A.prototype.constructor);//输出的是A函数(关注重点)
       
    3.function A(){
            var s = "mianqiang";
        }
        function B(){
       
        }
        function C(){}
        var a = new A();
        a.constructor = B;
        A.prototype.constructor = C;
        alert(a.constructor);//输出B
        alert(A.prototype.constructor);    //输出C
       
---
    function A(){}
    A.prototype.pa = 10;
    var a1 = new A();
    alert(a1.pa);//10
    a1.pa = 50;
    var a2 = new A();
    alert(a2.pa);//10
   
    function A(){}
    A.prototype = {pa:10};
    var a1 = new A();
    alert(a1.pa);//10
    a1.pa = 50;
    var a2 = new A();
    alert(a2.pa);//10
   
    function A(){}
    A.prototype.ca = {pa:10};
    var a1 = new A();
    alert(a1.ca.pa);//10
    //a1.ca = new A();
    a1.ca.pa = 50;//a1为实例,a1实例没有ca属性,所以a1.ca.pa这样的路径是直接影响在prototype的ca里的pa属性上;如果a1有ca属性(参看上面的注释句),则不会影响到prototype的ca里的pa属性上,而是影响在a1实例的ca对象的pa属性上,这种情况下最后a2.ca.pa输出的值则是10而不是50
    var a2 = new A();
    alert(a2.ca.pa);//50
   
    function A(){}
    A.prototype.ca = {pa:10};
    var a1 = new A();
    alert(a1.ca.pa);//10
    a1.ca = {ht:20};
    var a2 = new A();
    alert(a2.ca.pa);//10
   
    function A(){}
    A.prototype.ca = {pa:10};
    var a1 = new A();
    alert(a1.ca.pa);//10
    a1.ca = {ht:20};
    var a2 = new A();
    alert(a2.ca.ht);//undefined
   
    实例本身没有这个属性,但它的prototype有这个属性,如果为这个属性赋值,则实例会加上这个属性并且赋上值,而不是去修改prototype的这个属性值;但如果是去读取这个本身没有而prototype上有的属性,则会去调取prototype中这个属性的值
    -----------------------------
例子:   
function a(){
    this.cc = function(){alert("ac")};
}
a.prototype.bb = function(){
    alert("aa");
}

function b(){
    //mark 1: this.cc = function(){alert("bc")};
}
b.prototype.bb = function(){
    alert("bb");
}

b.prototype.constructor = a;    //这里将b的constructor转成函数a。此例子在于说明constructor的影响很小,更改成a并不会引起new b()会采用a去构造对象

var z1 = new b();
z1.bb();    //bb
z1.cc();    //cc is ,如果把mark 1处的注释去掉,则输出“bc”
-----------------------------------------------   

参考:

深入了解javascript中的prototype与继承

Js中Prototype、__proto__、Constructor、Object、Function关系介绍