- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
看完这个video我了解了一种用 Javascript 实现继承的好方法:原型继承。此方法使用 Object.create/Object.assign 根据另一个实例创建一个新对象。这看起来非常有趣,因为很容易理解正在发生的事情。例如:
const human = {
species: 'human',
create: function(values) {
const instance = Object.create(this);
Object.assign(instance, values);
return instance;
},
sayName: function() {
console.log(`Hi, my name is ${this.name}`);
},
saySpecies: function() {
console.log(`Hi, I'm a ${this.species}`);
}
}
const musician = human.create({
species: 'musician',
playInstrument: function() {
console.log(`I'm a musician and I play ${this.instrument}`);
}
});
const aHuman = human.create({ name: 'Paul' });
aHuman.sayName();
aHuman.saySpecies();
const aMusician = musician.create({ name: 'David', instrument: 'Guitar' });
aMusician.sayName();
aMusician.saySpecies();
aMusician.playInstrument();
// how to check if musician is an instance of human?
console.log(musician instanceof human);
通过经典继承,我可以使用 trường hợp của
来了解给定对象在其链中是否具有给定的 proto (例如 musician instanceof Human
>)。
Đây làproto链的样子,它看起来与使用mới
运算符创建的链非常相似。
câu hỏi如何通过原型(prototype)继承实现同样的效果?
1 Câu trả lời
您的尝试存在两个主要问题:
trường hợp của
的 RHS 必须是一个函数,因此您的类型必须构建在函数之上。tạo nên
是一种类型方法。 sayName
是一个实例方法。以下内容应该按您的预期工作。
function Human(){}
Object.assign(Human, {
create: function(values) {
let newObj = Object.create(this.prototype);
return Object.assign(newObj, values);
}
});
Object.assign(Human.prototype, {
species: 'human',
sayName: function() {
console.log(`Hi, my name is ${this.name}`);
},
saySpecies: function() {
console.log(`Hi, I'm a ${this.species}`);
}
});
function Musician(){}
Object.assign(Musician, Human);
Musician.prototype = Human.create({
species: 'musician',
playInstrument: function() {
console.log(`I'm a musician and I play ${this.instrument}`);
}
});
const aHuman = Human.create({ name: 'Paul' });
aHuman.sayName();
aHuman.saySpecies();
const aMusician = Musician.create({ name: 'David', instrument: 'Guitar' });
aMusician.sayName();
aMusician.saySpecies();
aMusician.playInstrument();
// how to check if musician is an instance of human?
console.log(aMusician instanceof Human);
关于javascript - 原型(prototype)继承: how to get instanceof?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46520434/
以下代码,我使用 chrome 浏览器控制台进行了检查: function A(){ this.a='a' } 这是一个构造函数。我已经将一个属性 b 赋给了 A 的原型(prototype)。
Hiện tại, câu hỏi này không phù hợp với định dạng Hỏi & Đáp của chúng tôi. Chúng tôi mong đợi câu trả lời được hỗ trợ bởi các sự kiện, tài liệu tham khảo hoặc chuyên môn, nhưng câu hỏi này có thể gây ra tranh luận, tranh cãi, thăm dò ý kiến hoặc thảo luận mở rộng. Nếu bạn cảm thấy vấn đề này có thể được cải thiện và có thể mở lại, hãy truy cập
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 5年前关闭。 Improve this
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 3年前关闭。 Improve this
我已经开始阅读 The Pragmatic Programmer,我很喜欢并学习堆形式,但我很难理解示踪子弹和原型(prototype)之间的区别。跟踪项目符号是否像拥有应用程序的所有 View 但尚
尽管阅读了 StackOverflow 上的大多数文章,但我现在实际上对原型(prototype)非常困惑。 function Foo() { } Foo.prototype.speak = func
我正在阅读以下代码,并开始想知道 Rectangle.prototype = Object.create(Shape.prototype) 和 Rectangle.prototype = Shape.
我想知道它们之间的区别: childObj.prototype = Object.create(parentObj.prototype) 和 childObj.prototype = parentOb
这个问题在这里已经有了答案: Why wouldn't I use Child.prototype = Parent.Prototype rather than Child.prototype =
在 node.js 中导出原型(prototype)的首选方法是什么?您可以采用两种方法: 导出原型(prototype)本身 function A () { } module.exports = A
我正在学习 JavaScript,发现了两种分配原型(prototype)的方法。 第一个是A.prototype = B.prototype,第二个是A.prototype = new B() 例如
在一些构造函数的定义之后,例如 child ,我见过以下两种形式: Child.prototype = Parent.prototype; 或 Child.prototype = new Parent
我正在阅读一本关于 OOP javascript 的书,但被其中一个示例卡住了。 在示例代码的第一个版本中,Shape 的一个新实例构造函数被创建并且 toString方法被调用。 toString方
这个问题在这里已经有了答案: What should I connect to the child prototype property in JavaScript (2 个答案) 关闭 8 年前。
在进行原型(prototype)设计时,您在多大程度上放弃了最佳实践来支持代码和修复黑客攻击?当然,代码并不打算在完整的生产环境中保留。 补充:我正在研究一个用 Python 制作的相当大的半工作原型
我正在尝试使用 Prototype 更新隐藏表单字段的值。表单域: 我正在尝试使用原型(prototype)更新值: var additionalVal = ',2'; var itemId = $
我正在阅读How to Make a Javascript Library我发现了作者所说的一个观点: function _() { //Some obects and var
我想用一个新函数扩展“Number”类型,因此我必须定义一个原型(prototype)。当我想到这一点时,我得到了一堆问题: Number 是否既继承了 Object.prototype 又继承了 F
这里好像有区别... 假设我们有 function MyConstructor() {} MyConstructor 的[[Prototype]] 是Function.prototype,不是 MyC
有人建议 Derived.prototype = Object.create(Base.prototype); 优于 Derived.prototype = new Base(); (如 this S
Tôi là một lập trình viên xuất sắc, rất giỏi!