var name = 'oop '
var Person = function (options) {
this.name = options.name
}//构造函数person
Person.prototype.name = 'Person'//构造函数person创建的实例原型的name属性
Person.prototype.getName = function () {
return this.name
}//构造函数person创建的实例原型的getName方法
Person.getName = function () {
return this.name;
}//构造函数的静态方法
var p = new Person({ name: 'inke' })//构造函数person创建的实例对象P
console.log(p.constructor === Person) // true
console.log(p instanceof Person) // true
console.log(p.__proto__ === Person.prototype) // true 每个对象都有一个__proto__属性 指向对象的原型 函数的prototype属性指向一个对象,这个对象正是调用该构造函数创建的实例原型
console.log(p.hasOwnProperty('name')) // true hasOwnProperty只能读取自己身上的属性
console.log(p.name) //inke.如果删除52行里name 则首先找到构造函数中的name 值为undefined
console.log(p.hasOwnProperty('getName')) // false
var getName = p.getName
console.log(getName === Person.getName) // ? false
console.log(getName()) // oop this此时指向全局
console.log(Person.prototype.getName()) // ? 'Person'
console.log(p.getName()) // 'inke' 如果删除52行 则首先找到构造函数中的person 值为undefined
console.log(Person.getName())// Person //函数的getName里的return this.name;返回函数的name属性 --函数名person
2021-10-08原型题目
最新推荐文章于 2023-09-05 16:46:56 发布