javascript之原型,原型链

1.定义:原型是function对象的一个属性,它定义了构造函数制造出的对象的公共祖先。通过该构造函数产生的对象,可以继承该原型的属性和方法。原型也是对象。

2.利用原型特点和概念,可以提取共有属性。

3.对象如何查看原型 — > 隐式属性 __proto__

4.对象如何查看对象的构造函数 — > constructor

 

var obj = {name:”a”}

var obj1 = obj;

obj = {name:”b”}

obj1.name = “a”;

绝大多数对象的最终都会继承自Object.prototype(特例:Object.create(null); )

Object.create(Object/null);  

 

原型链

//            Grand.prototype.__proto__ === Object.prototype -- >true
            Grand.prototype = {
                lastName: "abc"
            }

            function Grand() {
                age: 18
            }
            var grand = new Grand();
            Father.prototype = grand;

            function Father() {
                this.name = "bcd";
                this.card = {
                    card1:"card1"
                };
                this.num = 100;
            }
            var father = new Father();
            Son.prototype = father;

            function Son() {
                this.habbit = "smoking"
            }
            var son = new Son();
            //son.card.card2 = 'card2' -- >father.card2 == "card2"  (后辈访问祖先的对象添加属性时,会在祖先本身上修改)
            //son.num++;  father.num == 100; Son{num:101 }

猜你喜欢

转载自blog.csdn.net/duyujian706709149/article/details/88076820