黑马程序员前端JavaScript入门到精通全套视频教程,javascript核心进阶ES6语法、API、js高级等基础知识和实战教程
function Star(uname, age) {
this.uname = uname;
this.age = age;
this.sing = function() {
console.log('我会唱歌');
}
}
const ldh = new Star('刘德华', 18)
const zxy = new Star('张学友', 19)
console.log(ldh.sing === zxy.sing) // 结果为 false,说明俩函数不一样
function Star(uname, age) {
this.uname = uname;
this.age = age;
}
console.log(Star.prototype) // 返回一个对象称为原型对象
Star.prototype.sing = function() {
console.log('我会唱歌');
}
const ldh = new Star('刘德华', 18)
const zxy = new Star('张学友', 19)
console.log(ldh.sing === zxy.sing) // 结果为 true,说明俩函数一样,共享
function Star(name) {
this.name = name
}
Star.prototype = {
sing: function() { console.log('唱歌') },
dance: function() { console.log('跳舞') }
}
console.log(Star.prototype.constructor) // 指向 Object
function Star(name) {
this.name = name
}
Star.prototype = {
// 手动利用 constructor 指回 Star 构造函数
constructor: Star,
sing: function() { console.log('唱歌') },
dance: function() { console.log('跳舞') }
}
console.log(Star.prototype.constructor) // 指向 Star
function Man() {
this.head = 1;
this.eyes = 2;
this.legs = 2;
this.say = function() {},
this.eat = function() {}
}
const pink = new Man()
function Woman() {
this.head = 1;
this.eyes = 2;
this.legs = 2;
this.say = function() {},
this.eat = function() {},
this.baby = function() {}
}
const red = new Woman()
const People = {
head: 1;
eyes: 2;
legs: 2;
say: function() {},
eat: function() {}
}
// 男人
function Man() {
}
// 用 new Person() 替换刚才的固定对象
Man.prototype = new Person()
// 注意让原型里面的constructor重新指回Man
Man.prototype.constructor = Man
const pink = new Man()
Man.prototype.smoking = function() {}
console.log(pink)
// 女人
function Woman() {
this.bady = function() {}
}
// 用 new Person() 替换刚才的固定对象
Woman.prototype = new Person()
// 注意让原型里面的constructor重新指回Woman
Woman.prototype.constructor = Woman
const red = new Woman()
console.log(red)