* Rectangle继承Shape

  

function Shape() {
this.x = 0;
this.y = 0;
}
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
console.log("Shape moved ("+this.x+","+this.y+").");
}
function Rectangle() {
Shape.call(this);
}
// Rectangle.prototype = Object.create(Shape.prototype);
// Rectangle.prototype = new Shape.prototype.constructor();
Rectangle.prototype = new Shape(); var rect = new Rectangle();
console.log(rect instanceof Rectangle); // true
console.log(rect instanceof Shape); // true
rect.move(1,1); // Shape moved (1,1).

  

* 给定一个构造函数 constructor,请完成 alterObjects 方法,将 constructor 的所有实例的 greeting 属性指向给定的 greeting 变量。

input:

var C = function(name) {this.name = name; return this;}; 
var obj1 = new C('Rebecca'); 
alterObjects(C, 'What\'s up'); obj1.greeting;

output: What's up

var C = function(name) {
this.name = name;
return this;
}; var obj1 = new C('Rebecca'); function alterObjects(constructor, greeting) {
constructor.prototype.greeting = greeting;
} alterObjects(C, 'What\'s up');
console.log( obj1.greeting );

  

* 找出对象 obj 不在原型链上的属性(注意这题测试例子的冒号后面也有一个空格~)
1、返回数组,格式为 key: value
2、结果数组不要求顺序

input:

var C = function() {this.foo = 'bar'; this.baz = 'bim';};
C.prototype.bop = 'bip';
iterate(new C());

output:

["foo: bar", "baz: bim"]
function iterate(obj) {
var a = [];
for (var prop in obj) {
// if (obj.hasOwnProperty(prop)) {
if (!(prop in obj.__proto__)) {
a.push(prop + ": " + obj[prop]);
}
}
return a;
} var C = function() {
this.foo = 'bar';
this.baz = 'bim';
};
C.prototype.bop = 'bip';
console.log(iterate(new C())); // ["foo: bar", "baz: bim"]

  

* 一个jb51上的例子

// https://www.jb51.net/article/82587.htm
function SuperType(name) {
this.name = name;
this.property = "super type";
this.colors = ['red', 'blue', 'green'];
this.foo = function() {
console.log("SuperType this.foo");
}
}
SuperType.prototype.sayName = function () {
console.log(this.name);
}
SuperType.prototype.getSuperValue = function () {
return this.property;
}
SuperType.prototype.foo = function() {
console.log("SuperType.prototype.foo");
} function SubType(name, job) {
SuperType.call(this, name);
this.subproperty = "child type";
// this.foo = function() {console.log("SubType this.foo");}
this.job = job;
}
SubType.prototype = new SuperType();
SubType.prototype.constructor = SuperType;
SubType.prototype.sayJob = function() {
console.log(this.job);
} SubType.prototype.getSubValue = function () {
return this.subproperty;
};
SubType.prototype.foo = function() {
console.log("SubType.prototype.foo");
}; var instance = new SubType();
console.log( instance.getSuperValue() ); // super type
console.log( instance.getSubValue() ); // child type
console.log("-------------------");
console.log( instance.foo() ); // SuperType this.foo \n undfined
console.log("-------------------"); var instance1 = new SubType('Jiang', 'student')
instance1.colors.push('black')
console.log(instance1.colors) //["red", "blue", "green", "black"]
instance1.sayName() // 'Jiang'
instance1.sayJob() // 'student' var instance2 = new SubType('Vol\'jin', 'doctor')
console.log(instance2.colors) // //["red", "blue", "green"]
instance2.sayName() // Vol'jin
instance2.sayJob() // 'doctor' console.log("-------------------");
function createAnother(o) {
var clone = Object.create(o); /* 创建一个新对象 */
clone.sayHi = function() { /* 添加方法 */
console.log("hi");
}
return clone; /* 返回这个对象 */
}
var instance3 = createAnother(instance);
console.log(instance3.sayHi()); /* hi undefined */ console.log("-------------------"); // reset inheritance
SubType.prototype = {}; function inheritPrototype(subType, superType) {
var proto = Object.create(superType.prototype); // 创建父类原型的副本
proto.constructor = subType; // 将创建的副本添加constructor属性
subType.prototype = proto; // 将子类的原型指向这个副本
}
inheritPrototype(SubType, SuperType);
var instance4 = new SubType('Jiang', 'student');
instance4.sayName();

  

javascript 继承 inheritance prototype的更多相关文章

  1. javascript继承(五)—prototype最优两种继承(空函数和循环拷贝)

    一.利用空函数实现继承 参考了文章javascript继承—prototype属性介绍(2) 中叶小钗的评论,对这篇文章中的方案二利用一个空函数进行修改,可以解决创建子类对象时,父类实例化的过程中特权 ...

  2. javascript继承(四)—prototype属性介绍

    js里每一个function都有一个prototype属性,而每一个实例都有constructor属性,并且每一个function的prototype都有一个constructor属性,这个属性会指向 ...

  3. javascript继承—prototype最优两种继承(空函数和循环拷贝)

    一.利用空函数实现继承 参考了文章javascript继承-prototype属性介绍(2) 中叶小钗的评论,对这篇文章中的方案二利用一个空函数进行修改,可以解决创建子类对象时,父类实例化的过程中特权 ...

  4. 谈谈javascript中的prototype与继承

    谈谈javascript中的prototype与继承 今天想谈谈javascript中的prototype. 通常来说,javascript中的对象就是一个指向prototype的指针和一个自身的属性 ...

  5. Javascript 继承 call与prototype

    function Parent(hello){ this.hello = hello; this.sayHello = function(){ alert(this.hello); } } Paren ...

  6. JavaScript强化教程——Cocos2d-JS中JavaScript继承

    javaScript语言本身没有提供类,没有其它语言的类继承机制,它的继承是通过对象的原型实现的,但这不能满足Cocos2d-JS引擎的要求.由于Cocos2d-JS引擎是从Cocos2d-x演变而来 ...

  7. [原创]JavaScript继承详解

    原文链接:http://www.cnblogs.com/sanshi/archive/2009/07/08/1519036.html 面向对象与基于对象 几乎每个开发人员都有面向对象语言(比如C++. ...

  8. Cocos2d-JS中JavaScript继承

    JavaScript语言本身没有提供类,没有其它语言的类继承机制,它的继承是通过对象的原型实现的,但这不能满足Cocos2d-JS引擎的要求.由于Cocos2d-JS引擎是从Cocos2d-x演变而来 ...

  9. JavaScript继承的实现

    怎样在JavaScript中实现简单的继承?  以下的样例将创建一个雇员类Employee,它从Person继承了原型prototype中的全部属性. function Employee(name, ...

随机推荐

  1. 从MySQL大量数据清洗到TiBD说起

    从MySQL大量数据清洗到TiBD说起 一.业务场景: 公司主要做的业务是类似贝壳的二手房租售,公司数据库存了上亿级别的房源数据,之前的数据库使用的是 mysql,后面需要将mysql数据库切换成了 ...

  2. noip13

    T1 一开始直接丢了个暴力走人50pts,然后开始打表找规律,啥也没找着,最后二十分钟突然看出来了什么,把 \(f_{n,m}\)式子列了一下,发现常数项没啥规律,最后五分钟,突然闪过一丝灵感,但是是 ...

  3. centos上安装zookeeper

    下载zookeeper # 创建zookeeper文件夹 cd /usr/local/ mkdir zookeeper cd zookeeper # 下载 解压 wget https://mirror ...

  4. 带你读AI论文丨LaneNet基于实体分割的端到端车道线检测

    摘要:LaneNet是一种端到端的车道线检测方法,包含 LanNet + H-Net 两个网络模型. 本文分享自华为云社区<[论文解读]LaneNet基于实体分割的端到端车道线检测>,作者 ...

  5. nodejs根据word模板生成文档(方法二)

    [推荐该方法,模板比较简洁] 1,代码, 这里采用的模块为 docxtemplater 和 open-docxtemplater-image-module,均为开源(docxtemplater 有收费 ...

  6. Dijkstra链路状态选路算法

  7. MAC下Jetbrains编译器无法打开问题解决

    这段时间不知道怎么回事,每次打开Rider必定闪退,毫无头绪,只好暂时放弃使用Rider,试用了一段时间Visual Studio. 可惜...虽然大学时候觉得VS天下第一,但是用惯了JB的编译器,再 ...

  8. UVA1620 Lazy Susan(结论证明)

    结论: 当 \(n\geq 6\) 时,若 \(n\) 是奇数且输入序列的逆序对数是奇数,则无解,否则有解. 当 \(n=4\) 或 \(n=5\) 时,答案个数及其有限,只有这个环是 \(1\) 到 ...

  9. docker容器 如何精简镜像减小体积

    写在前面 我们在上篇<Docker容器 关于镜像构建的安全问题>一起学习了如何构建一个基于安全的镜像,这篇小作文我们会学习镜像构建的另一个关键性问题,为何别人打造的镜像只有10MB而我的有 ...

  10. java一些工具类

    import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java. ...