Problems with the display of attributes defined by Object.defineProperty in Google browser

1 the code is as follows

function Vue () {
    this._init()
}    

Vue.prototype._init = function () {
    let vm = this
}
Object.defineProperty(Vue.prototype, "$ssrContext", {
    get () {
        return false
    }
})

const vm = new Vue()

2 after instantiation, you can see the following

in the browser.

3 question
the same method defined on the prototype uses the Object.defineProperty method. Why is there a $ssrContext property under the current instance? is it the reason for the browser implementation?

May.13,2022

take a look at MDN's description of get

follow that there is a sentence here

The
get syntax binds an object property to the function that will be called when the property is queried.

then when you new an instance, there are actually some implied operations
when function A directly return an object, the value of b is the object from A return, and it does not contain the object
from A's prototype because the object from return is a new object, and it has nothing to do with A's prototype.

for example, the following code:

function A () { return { age: 22 } }
A.prototype.test = 'hello'
var b = new A()
// b => { age: 22 }
//  b.test 

but if there is no explicit return of an object, a new object is created, then the this scope of the current function (A) is bound to the object, the code in the function (A) is executed, and the prototype (*) is accessed. Combined with the description of MDN above, you can see why the extra properties are.

but the extra attributes here are a little weird
for example, the following code

function A () {}
A.prototype._init = function() { console.log('init') }
Object.defineProperty(A.prototype, 'test', {
    get() {
        console.log(this)
        return 24
    }
})

var b = new A()

A.prototype.hasOwnProperty('test')
// => true
A.prototype.hasOwnProperty('_init')
// => true

A.hasOwnProperty('test')
// => false

/ / although test seems to be an attribute on A, it does not explicitly belong to A

.


because you created it on the prototype chain, of course instantiation has


Object.defineProperty(Vue.prototype, '$ssrContext', {
    value: false
})

if you write in this way, there is no such thing.

Menu