javascript继承之原型链(一)
JavaScript继承 原型
2023-09-11 14:22:25 时间
1 function Father() { 2 this.fatherValue = "爸爸"; 3 } 4 Father.prototype.getFatherValue = function () { 5 return this.fatherValue 6 } 7 function Son() { 8 this.sonValue = "儿子"; 9 } 10 Son.prototype = new Father(); 11 Son.prototype.getSonValue = function () { 12 return this.sonValue; 13 } 14 var xiaoMing = new Son(); 15 alert(xiaoMing.getSonValue());
先实例化father,并且把这个对象赋值给son的原型.
son继承了father之后,再修改原型,添加getSonValue()方法.
通过原型来实现继承时,原型实际上会变成另一个类型的实例.于是,原先的实例属性也就顺利成章的变成了现在的原型属性.
这里存在一个问题,子类son无法向父类father中传参.这个问题会在下一章的借用构造函数继承中有所优化.
相关文章
- JavaScript
- 在JavaScript中使用json.js:Ajax项目之GET请求(同步)
- 【JavaScript】对象
- [Javascript] Link to Other Objects through the JavaScript Prototype Chain (Object.setPrototypeOf())
- [Javascript] Natively Format JavaScript Dates and Times
- [Javascript] Hide Properties from Showing Up in "for ... in" Loops in JavaScript
- [Javascript] Intercept property access with Javascript Proxy
- 理解JavaScript中的事件处理
- 单线程的JavaScript
- 面向对象的Javascript(5):继承
- [Javascript] Cancel a promise
- [Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures
- [Javascript] Conditionally spread entries to a JavaScript object
- [Javascript] Multiply Two Arrays over a Function in JavaScript
- [Javascript] Use JavaScript's for-in Loop on Objects with Prototypes
- [Javascript] Link to Other Objects through the JavaScript Prototype Chain
- [Javascript] Compose multiple functions for new behavior in JavaScript
- [Javascript] Iterate Over Items with JavaScript's for-of Loop
- [Javascript] What is JavaScript Function Currying?
- [Javascript] The Array map method
- 一个百度贴吧下载指定单个帖子所有回复的工具(JavaScript)实现
- JavaScript 有多灵活?
- Javascript中的几种继承方式比较
- Javascript中的几种继承方式比较
- Atitit.实现继承的原理and方法java javascript .net c# php ...
- javascript案例17——留言发布、简易发布留言案例
- javascript设计模式理论和示例深入分析(上)
- 从零开始学_JavaScript_系列(18)——dojo(7)(dojo中类的继承)
- JavaScript中的parseInt和Number函数
- JavaScript学习总结(四)——this、原型链、javascript面向对象
- JavaScript面向对象编程(10)高速构建继承关系之对象拷贝