原型对象添加属性和赋值对象的区别

我们知道,原型对象在js中有很重要的地位,我们经常会用它,为他添加属性或者方法,以及用一个全新的变量(大部分是对象)赋值给它,在此我讨论的是为原型对象添加属性以及赋值对象的区别

原型对象由构造函数的prototype引用而得,而且这个对象会自带一个不可枚举constructor属性,值为构造函数本身。如下

            let Test = function(){
                this.type="test"
            }
            Test.prototype.age = 10;
            let exp = new Test();
            console.log(Test.prototype.constructor===Test) //true
            console.log(exp.constructor===Test)  //true

此时实例上的constructor属性并非自身属性而是从其父类原型上获取的

当我们为原型对象添加属性时,由于没有也不会手动操作constructor属性,故原型的constructor属性不会变动,但是当我们将另一个对象赋值给原型对象时,此时需要注意赋值对象是否有constructor属性且是否需要更改

      let Test = function(){
                this.type="test"
            }
            Test.prototype.age = 10;
            let exp = new Test();
          //此时为原型对象赋值新对象
            Test.prototype = {
                content:"新内容"
            }
            console.log(Test.prototype.constructor)  //ƒ Object() { [native code] }
            console.log(Test.prototype.constructor===Object)  //true,因为对象直接量的
//constructor属性由在其原型链上取为Object
           //此时我们需要矫正原型对象,如下
            //1.
                Test.prototype.constructor = Test  //可以被枚举
            //2.
                Test.prototype = {
                    constructor : Test,   //对象直接量中补充该属性,同样可以被枚举
                    content:"新内容"
                }
            //3. 使用Object.defineProperty,令属性不可枚举,符合要求
                Object.defineProperty(Test.prototype,"constructor ",{
                    value : Test,
                    enumerable : false
                 })

除了上述关于constructor属性的区别外,我还发现为原型对象添加属性或者赋予对象,其代码前面的new出来的实例有差别
添加属性:

      let Test = function(){
                this.type="test"
            }
            Test.prototype.age = 10;
            let exp = new Test();
            console.log("前")
            console.log(exp)
          //此时为原型对象添加属性
            Test.prototype.sex = "man";
            console.log("后")
            console.log(exp)

           //前后都输出:
            Test
                type: "test"
                __proto__:
                    age: 10
                    sex: "man"
                    constructor: ƒ ()
                    __proto__: Object

赋予对象:

            let Test = function(){
                this.type="test"
            }
            Test.prototype.age = 10;
            let exp = new Test();
            console.log("前")
            console.log(exp)

            //输出
            Test
                type: "test"
                __proto__:
                    age: 10
                    constructor: ƒ ()
                        arguments: null
                        caller: null
                        length: 0
                        name: "Test"
                        prototype: {content: "新内容"}
                        ......

            Test.prototype = {
                content:"新内容"
            }
            console.log("后")
            console.log(exp) 

            //输出
            Test
                type: "test"
                __proto__:
                    age: 10
                    constructor: ƒ ()
                        arguments: null
                        caller: null
                        length: 0
                        name: "Test"
                        prototype: {content: "新内容"}
                        ......

            console.log(exp.age)  //10,即使原型赋予新对象,依然可以访问之前的属性

            let newExp = new Test();
            console.log("newExp")
            console.log(newExp)
            //输出
            Test
                type: "test"
                __proto__:
                    content: "新内容"
                    __proto__: Object

结论:单纯为类的prototype添加属性或者则跟着变,若为其赋值为新对象,则不会及时变更,且依然能访问变更前的属性。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容