读《JavaScript面向对象编程指南》(二)
2024-08-30 07:28:26
第五章 原型
- 在JavaScript中,所有函数都会拥有一个 prototype 的属性,默认初始值为空对象。
- 可以在相关的原型对象中添加新的方法和属性,甚至可以用自定义对象来完全替换掉原有的原型对象。
- 通过某个构造器函数来new一个对象时,这些对象就会自动拥有一个指向 prototype 属性的 __proto__链接,通过它可以访问相关原型对象的属性。
- 对象自身属性的优先级要高于其原型对象的同名属性。
- 扩展内建对象不是一个好主意,如果必须要采用的话一定要谨慎。
- 当我们对原型对象执行完全替换时,可能会触发原型链某种异常,prototype.constructor属性是不可靠的。
- Best Practice:当我们重写某对象的prototype时,重置相应的constructor属性是一个好习惯。
- 每个对象都会有一个isprototype()方法,这个方法会告诉我们当前对象是否是另一个对象的原型。
- 神秘的__proto__链接。
枚举属性
参考链接:JavaScript for...in循环。
第六章 继承
//6.1.1 原型链示例
//6.1.2 将共享属性迁移到原型中去
//6.2 只继承于原型
function Shape(){ }
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
return this.name;
}
function TwoDShape(){ }
TwoDShape.prototype = Shape.prototype;
TwoDShape.prototype.constructor = TwoDShape;
TwoDShape.prototype.name = '2D shape'; function Triangle(side, height){
this.side = side;
this.height = height;
}
Triangle.prototype = TwoDShape.prototype;
Triangle.prototype.constructor = Triangle;
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function(){
return this.side * this.height / 2;
}
//临时构造器 new F()
function Shape(){ }
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
return this.name;
}
function TwoDShape(){ }
var F = function(){};
F.prototype = Shape.prototype;
TwoDShape.prototype = new F();
TwoDShape.prototype.constructor = TwoDShape;
TwoDShape.prototype.name = '2D shape'; function Triangle(side, height){
this.side = side;
this.height = height;
}
var F = function(){};
F.prototype = TwoDShape.prototype;
Triangle.prototype = new F();
Triangle.prototype.constructor = Triangle;
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function(){
return this.side * this.height / 2;
} //6.3 uber——子对象访问父对象的方式
function Shape(){}
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
var result = [];
if (this.constructor.uber){
result[result.length] = this.constructor.uber.toString();
}
result[result.length] = this.name;
return result.join(', ');
}
function TwoDShape(){ }
var F = function(){};
F.prototype = Shape.prototype;
TwoDShape.prototype = new F();
TwoDShape.prototype.constructor = TwoDShape;
TwoDShape.uber = Shape.prototype;
TwoDShape.prototype.name = '2D shape'; function Triangle(side, height){
this.side = side;
this.height = height;
}
var F = function(){};
F.prototype = TwoDShape.prototype;
Triangle.prototype = new F();
Triangle.prototype.constructor = Triangle;
Triangle.uber = TwoDShape.prototype;
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function(){
return this.side * this.height / 2;
} // 6.4 将继承部分封装成函数
function extend(Child, Parent){
var F = function(){};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.uber = Parent.prototype;
}
extend(TwoDShape, Shape);
extend(Triangle, TwoDShape); //6.5 属性拷贝
function extend2(Child, Parent){
var p = Parent.prototype;
var c = Child.prototype;
for (var i in p){
c[i] = p[i];
}
c.uber = p;
} //6.6 小心处理引用拷贝
//6.7 对象之间的继承
function extendCopy(p){
var c = {};
for(var i in p){
c[i] = p[i];
}
c.uber = p;
return c;
} //6.8 深拷贝
function deepCopy(c, p){
var c = c || {};
for (var i in p){
if (typeof p[i] === 'object'){
c[i] = (p[i].constructor === Array) ? [] : {};
deepCopy(c[i], p[i])
}else{
c[i] = p[i];
}
}
return c;
}
//6.9 object(),Douglas Crockford为我们提出了一个建议,既可以用object()
//函数来接受父对象,并返回一个以该对象为原型的新对象。
function object()(o){
var n;
function F(){}
F.prototype = o;
n = new F();
n.uber = o;
return n;
} //6.10 原型继承于属性拷贝的混合使用
function objectPlus(o, stuff){
var n;
function F(){};
F.prototype = o;
n = new F();
n.uber = o; for(var i in stuff){
n[i] = stuff[i];
}
return n;
} //6.11 多重继承
function multi(){
var n = {}, stuff, j = 0, len = arguments.length;
for(j = 0; j < len; j++){
stuff = arguments[j];
for(var i in stuff){
n[i] = stuff[i];
}
}
return n;
} //6.12 寄生式继承
var twoD = {
name : '2D shape',
dimensions : 2
}
function triangle(s, h){
var that = object(twoD);
that.name = 'Triangle';
that.getArea = function(){
return this.side * this.height / 2
};
that.side = s;
that.height = h;
return that;
} //6.13 构造器借用
function Shape(id){
this.id = id;
}
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
return this.name;
}
function Triangle(){
Shape.apply(this, arguments);
}
Triangle.prototype = new Shape();
Triangle.prototype.name = 'Triangle';
//消除双重继承
function Shape(id){
this.id = id;
}
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
return this.name;
}
function Triangle(){
Shape.apply(this, arguments);
}
function extend2(Child, Parent){
var p = Parent.prototype;
var c = Child.prototype;
for (var i in p){
c[i] = p[i];
c.uber = p;} }
extend2(Triangle, Shape);
Triangle.prototype.name = 'Triangle';
最新文章
- 尝试使用Memcached遇到的狗血问题
- Sqrtx
- 集成ShareSDK,分享成功后QQ和空间回调不执行的可能原因
- 鸟哥的linux私房菜学习笔记 __ 命令与文件的搜寻
- Mysql_mysql多个TimeStamp设置
- CCF真题之Z字形扫描
- model.addAttribute(";student";,student)——渲染
- c语言知识点总结(摘自head first c)
- Eyeshot Ultimate 学习笔记(2)
- java笔记11之二维数组
- Android provider authorities冲突
- poj 3190 Stall Reservations 贪心 + 优先队列
- 为什么Intent传递对象的时候必须要将对象序列化呢?
- 【原创】Spring MVC项目搭建(使用Java配置)
- 多线程面试题系列(8):经典线程同步 信号量Semaphore
- 几个常用的文本处理shell 命令:find、grep、sort、uniq、sed、awk
- 用premake5创建lua532工程
- API知识点总结
- TODO 软件测试68题
- [COCI2015]COCI
热门文章
- 判断对象当中有没有某一个属性(AS,JS,Java语言比较)
- 电脑上不了网,但是能登录QQ 问题解决方案
- spring boot系列(一)spring boot 初识
- Eclipse进行Debug时断点上有一个斜杠,并且debug没有停在断点处
- Xcode真机报错clang: error: linker command failed with exit code 1 (use -v to see invocation)
- centos 7.3镜像制作
- Qt qss问题总结
- 【ABAP系列】SAP ABAP 带有参数的AMDP的创建
- Leetcode之动态规划(DP)专题-877. 石子游戏(Stone Game)
- RFC3550中文