js继承
来源:互联网 发布:python split删除空格 编辑:程序博客网 时间:2024/06/03 17:15
var o = { a: 2, m: function(){ return this.a + 1; }};console.log(o.m()); // 3// 当调用 o.m 时,'this'指向了o.var p = Object.create(o);// p是一个对象, p.[[Prototype]]是o.p.a = 12; // 创建 p 的自身属性a.console.log(p.m()); // 13// 调用p.m
时, 'this'指向 p. // 又因为 p 继承 o 的 m 函数// 此时的'this.a'
即 p.a,即 p 的自身属性 'a'
function A(a){ this.varA = a;}// 以上函数 A 的定义中,既然 A.prototype.varA 总是会被 this.varA 遮蔽,// 那么将 varA 加入到原型(prototype)中的目的是什么?A.prototype = { varA : null, // 既然它没有任何作用,干嘛不将 varA 从原型(prototype)去掉? //
也许作为一种在隐藏类中优化分配空间的考虑?// https://developers.google.com/speed/articles/optimizing-javascript#Initializing instance variables // 将会验证如果 varA 在每个实例不被特别初始化会是什么情况。 doSomething : function(){ // ... }}function B(a, b){ A.call(this, a); this.varB = b;}B.prototype = Object.create(A.prototype, { varB : { value: null, enumerable: true, configurable: true, writable: true }, doSomething : { value: function(){ // override A.prototype.doSomething.apply(this, arguments); // call super // ... }, enumerable: true, configurable: true, writable: true }});B.prototype.constructor = B;var b = new B();b.doSomething();
最重要的部分是:
- 类型被定义在
.prototype 中
- 而你用
Object.create()
来继承
1 0
- JS继承--组合继承
- JS继承-类继承
- Js继承。
- js继承
- js继承
- js 继承
- js继承
- JS 继承
- JS 继承
- js继承
- js继承
- JS继承
- JS继承
- js继承
- JS继承
- JS继承
- js继承
- JS继承
- 异常记录
- 使用dbcd删除后重建实例
- Android编译过程中预拷贝文件或文件夹
- js创建jsonArray
- 快速利用第三方平台SDK,实现点对点语音通话
- js继承
- Spring学习笔记---5-Spring事务管理(1)
- Hi 各位C友,如果您有技术性的问题,请移步至以下连接:如果你是一位技术工城狮,相信你一定会喜欢的!
- 逐渐新增N个Mnesia节点,并确保数据在这些节点上保持同步.
- 龍雲工作室用户隐私策略
- 爬虫之京东年度账单
- 里氏替换原则
- Python网络编程
- Android inject input events 注入Touch 点(x, y) 触摸输入事件