读《JavaScript: the good parts》, 关于对象继承这块小记一笔:

function Base(v){
this.baseValue = v;
this.getBaseValue = function(){
return this.baseValue;
};
} function TestObject(base, c){
var temp = function(){
this.content = c;
this.fromBaseValue = function(){
return this.baseValue;
};
}; temp.prototype = base; return new temp();
} var b1 = new Base(1); var t1 = new TestObject(b1, 'this is t1');
var t2 = new TestObject(b1, 'this is t2'); console.log('t1', {'baseValue':t1.baseValue, 'fromBaseValue()': t1.fromBaseValue(), 'getBaseValue()':t1.getBaseValue()})
console.log('t2', {'baseValue':t2.baseValue, 'fromBaseValue()': t2.fromBaseValue(), 'getBaseValue()':t2.getBaseValue()})
//t1 { baseValue: 1, 'fromBaseValue()': 1, 'getBaseValue()': 1 }
//t2 { baseValue: 1, 'fromBaseValue()': 1, 'getBaseValue()': 1 } b1.baseValue = 2 console.log('t1', {'baseValue':t1.baseValue, 'fromBaseValue()': t1.fromBaseValue(), 'getBaseValue()':t1.getBaseValue()})
console.log('t2', {'baseValue':t2.baseValue, 'fromBaseValue()': t2.fromBaseValue(), 'getBaseValue()':t2.getBaseValue()})
//t1 { baseValue: 2, 'fromBaseValue()': 2, 'getBaseValue()': 2 }
//t2 { baseValue: 2, 'fromBaseValue()': 2, 'getBaseValue()': 2 } t1.baseValue = 3;
//If we try to retrieve a property value from a object, and if the object lacks the property name,
//then JavaScript attemps to retrieve the property value from the prototype object.
console.log('t1', {'baseValue':t1.baseValue, 'fromBaseValue()': t1.fromBaseValue(), 'getBaseValue()':t1.getBaseValue()})
console.log('t2', {'baseValue':t2.baseValue, 'fromBaseValue()': t2.fromBaseValue(), 'getBaseValue()':t2.getBaseValue()})
//t1 { baseValue: 3, 'fromBaseValue()': 3, 'getBaseValue()': 3 }
//t2 { baseValue: 2, 'fromBaseValue()': 2, 'getBaseValue()': 2 } b1.baseValue = 4 console.log('t1', {'baseValue':t1.baseValue, 'fromBaseValue()': t1.fromBaseValue(), 'getBaseValue()':t1.getBaseValue()})
console.log('t2', {'baseValue':t2.baseValue, 'fromBaseValue()': t2.fromBaseValue(), 'getBaseValue()':t2.getBaseValue()})
//t1 { baseValue: 3, 'fromBaseValue()': 3, 'getBaseValue()': 3 }
//t2 { baseValue: 4, 'fromBaseValue()': 4, 'getBaseValue()': 4 }

JavaScript 继承机制小记的更多相关文章

  1. javascript继承机制的设计思想(ryf)

    我一直很难理解Javascript语言的继承机制. 它没有"子类"和"父类"的概念,也没有"类"(class)和"实例" ...

  2. 【JavaScript】重温Javascript继承机制

    上段时间,团队内部有过好几次给力的分享,这里对西风师傅分享的继承机制稍作整理一下,适当加了些口语化的描述,留作备案. 一.讲个故事吧 澄清在先,Java和Javascript是雷锋和雷峰塔的关系.Ja ...

  3. 转:Javascript继承机制的设计思想

    我一直很难理解Javascript语言的继承机制. 它没有"子类"和"父类"的概念,也没有"类"(class)和"实例" ...

  4. Javascript继承机制的设计思想

    转自:http://www.ruanyifeng.com/blog/2011/06/designing_ideas_of_inheritance_mechanism_in_javascript.htm ...

  5. javascript 继承机制设计思想

    作者: 阮一峰 原文链接:http://www.ruanyifeng.com/blog/2011/06/designing_ideas_of_inheritance_mechanism_in_java ...

  6. Javascript继承机制总结 [转]

    转自:http://bbs.csdn.net/topics/260051906 Javascript继承 一直想对Javascript再次做一些总结,正好最近自己写了一个小型Js UI库,总结了一下J ...

  7. javascript继承机制 & call apply使用说明

    一.继承机制 1.对象冒充:构造函数使用 this 关键字给所有属性和方法赋值,可使 ClassA 构造函数成为 ClassB 的方法,然后调用它. function ClassZ() { this. ...

  8. Javascript继承机制的设计

    写软工作业时各种蛋疼:主要在于Javascript没有“子类”“父类”“接口”的概念,只能使用prototype来实现,看了下面一篇文章,感觉写得很不错~ http://www.ruanyifeng. ...

  9. Javascript prototype 及 继承机制的设计思想

    我一直很难理解Javascript语言的继承机制. 它没有"子类"和"父类"的概念,也没有"类"(class)和"实例" ...

随机推荐

  1. [转]让程序在崩溃时体面的退出之CallStack

    原文地址:http://blog.csdn.net/starlee/article/details/6618849 在我的那篇<让程序在崩溃时体面的退出之Unhandled Exception& ...

  2. linux 内核驱动加载过程中 向文件系统中的文件进行读写操作

    utils.h 文件: #ifndef __UTILS_H__ #define __UTILS_H__ void a2f(const char *s, ...); #endif utils.c 文件: ...

  3. MongoDB简单操作

    Hadoop核心技术厂商Cloudera将在2014/06推出hadoop Ecosystem与MongoDB的整合产品,届时MongoDB与ipmala及hbase,hive一起用; 开源linux ...

  4. nyoj 329 循环小数【KMP】【求最小循环节长度+循环次数+循环体】

    循环小数 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 我们可爱的 c小加 近段儿正在潜心研究数学,当他学习到循环小数这一部分时不是太明白循环体是什么意思(比如说3 ...

  5. Please check if the Publishing Tools on the server (System/PublishingTools) are started.

    ArcMap或ArcCatalog中双击连接到Server即可,该工具即自动可启动

  6. 使用Python扫描端口情况

    #!/usr/bin/python# -*- coding:utf8 -*-# Python:          2.7.8# Platform:        Windows# Authro:    ...

  7. Away3D 的实体收集器流程1

    View3D 顾名思义 它就是一个3D视口,视口的默认值是无穷大的,即相当于“窗口”是无限大的,我们看到的将是外面的所有景物即flash尺寸是视口的有效视域.传统电视的高宽比为4:3,现在的高清电视的 ...

  8. OC的单例模式的实现

    下面是在ARC,GCD下的单例模式实现: 头文件里申明类方法getInstance: #import <Foundation/Foundation.h> @interface Single ...

  9. RGB的三维模型与渐变色-颜色系列之一

    一.前言 以下与颜色相关的日志记录了俺学习颜色的有关容,限于编写时的水平,难免存在缺点与错误,希望得到朋友.同行和前辈的指教,非常感谢.1.  RGB的三维模型与渐变色-颜色系列之一2.  <颜 ...

  10. UVa 993: Product of digits

    这道题很简单.先将N用2,3,5,7(即10以内的素数)分解因数(需要先特殊判断N不为1),然后将可以合并的因数合并(如2*2合并成4,)这样求得的结果位数会减少,大小肯定会小一些.具体实现见代码. ...