
function foo () {
}
foo.prototype.name = "123"
var p = new foo()
//查找对象所有属性
//Object.getOwnPropertyNames(foo.prototype)
console.log(p.__proto__.constructor.name, Object.getOwnPropertyNames(p.__proto__))
console.log(foo.prototype.constructor.name)
PS D:\myName\js高级> node 函数原型链.js
{ name: '123' } [ 'constructor', 'name' ]
PS D:\myName\js高级> node 函数原型链.js
[Function: foo] [ 'constructor', 'name' ]
PS D:\myName\js高级> node 函数原型链.js
foo [ 'constructor', 'name' ]
1.构造函数赋值新的对象
function foo () {
}
foo.prototype = {
name: 1231
}
Object.defineProperty(foo.prototype, "constructor", {
writable: true,
enumerable: false,
configurable: true,
value: foo //添加函数
})
var p = new foo()
//查找对象所有属性
//Object.getOwnPropertyNames(foo.prototype)
console.log(Object.getOwnPropertyNames(p.__proto__))
console.log(foo.prototype.constructor.name)
2.创造对象原型和构造函数.js
function foo (name, age, height, address) {
this.name = name
this.age = age
this.height = height
this.address = address
}
foo.prototype.eat = function () {
console.log(this.name)
}
var F1 = new foo('张三', 18, 165, "北京")
var F2 = new foo('王五', 18, 165, "北京")
F1.eat()
F2.eat()
1605

被折叠的 条评论
为什么被折叠?



