Object.defineproperty 如何作用到类上或者说构造函数上 。试了下好像只能作用的对象实例上
function Person(name,age) {
this._name=name
this._age=age
}
Person.prototype.say=function(){
console.log(this._name)
}
let huixu = new Person('huixu',18)
let lili = new Person('lili',25)
Object.defineProperty(huixu,"_name",{value:'xxx'})//作用在对象实例上是有效的
//Object.defineProperty(Person,"_name",{value:'xxx'}) 无效
huixu.say()//huixu
lili.say()//lili
这么定义属性是有效的,不管是上面说的构造函数,还是对象。但并不是定义在原型上,所以对应的实例对象无法继承。
如果直接在Person.prototype 原型上定义属性,就可以看到效果。