javascript 继承 inheritance prototype
* 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的更多相关文章
- javascript继承(五)—prototype最优两种继承(空函数和循环拷贝)
一.利用空函数实现继承 参考了文章javascript继承—prototype属性介绍(2) 中叶小钗的评论,对这篇文章中的方案二利用一个空函数进行修改,可以解决创建子类对象时,父类实例化的过程中特权 ...
- javascript继承(四)—prototype属性介绍
js里每一个function都有一个prototype属性,而每一个实例都有constructor属性,并且每一个function的prototype都有一个constructor属性,这个属性会指向 ...
- javascript继承—prototype最优两种继承(空函数和循环拷贝)
一.利用空函数实现继承 参考了文章javascript继承-prototype属性介绍(2) 中叶小钗的评论,对这篇文章中的方案二利用一个空函数进行修改,可以解决创建子类对象时,父类实例化的过程中特权 ...
- 谈谈javascript中的prototype与继承
谈谈javascript中的prototype与继承 今天想谈谈javascript中的prototype. 通常来说,javascript中的对象就是一个指向prototype的指针和一个自身的属性 ...
- Javascript 继承 call与prototype
function Parent(hello){ this.hello = hello; this.sayHello = function(){ alert(this.hello); } } Paren ...
- JavaScript强化教程——Cocos2d-JS中JavaScript继承
javaScript语言本身没有提供类,没有其它语言的类继承机制,它的继承是通过对象的原型实现的,但这不能满足Cocos2d-JS引擎的要求.由于Cocos2d-JS引擎是从Cocos2d-x演变而来 ...
- [原创]JavaScript继承详解
原文链接:http://www.cnblogs.com/sanshi/archive/2009/07/08/1519036.html 面向对象与基于对象 几乎每个开发人员都有面向对象语言(比如C++. ...
- Cocos2d-JS中JavaScript继承
JavaScript语言本身没有提供类,没有其它语言的类继承机制,它的继承是通过对象的原型实现的,但这不能满足Cocos2d-JS引擎的要求.由于Cocos2d-JS引擎是从Cocos2d-x演变而来 ...
- JavaScript继承的实现
怎样在JavaScript中实现简单的继承? 以下的样例将创建一个雇员类Employee,它从Person继承了原型prototype中的全部属性. function Employee(name, ...
随机推荐
- 题解 d
传送门 写出来\(n^2\)就有81pts-- \(n^2\)的话枚举最后成为限制的是哪两个矩形,利用前缀和和二分\(n^3\)化\(n^2\)就行了 这题最无脑直接贪心的方法会有后效性 但实际上正解 ...
- redisson 分布式加锁
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- 单例对象 (Singleton)设计模式
单例的目的是为了保证运行时Singleton类只有唯一的一个实例,用于一些较大开销的操作. 饿汉式(没有线程安全问题): ' 由于使用static关键字进行了修饰,只能获取到一个对象,从而达到了单例, ...
- C++泛型编程之类模板
泛型语义 泛型(Generic Programming),即是指具有在多种数据类型上皆可操作的含意.泛型编程的代表作品 STL 是一种高效.泛型.可交互操作的软件组件. 泛型编程最初诞生于 C++中, ...
- MySQL时间戳、字符串、日期
1.时间转字符串:date_format(date, format) SELECT date_format(now(), '%Y-%m-%d') 2.时间转时间戳:unix_timestamp() S ...
- JAVA虚拟机中的堆内存Heap与栈内存Stack
原文链接:http://www.cnblogs.com/laoyangHJ/archive/2011/08/17/gc-Stack.html 深入Java虚拟机:JVM中的Stack和Heap 在JV ...
- Redis(二):基本数据类型
基础 # redis默认有16个数据库,数组下标从0开始,默认使用0号库 # 当我们启动服务器并连接客户端之后: set <key> <value> # 向数据库中添加数据用于 ...
- 接口测试进阶接口脚本使用--apipost(预/后执行脚本)
预执行脚本的作用时间 预执行脚本是一个请求发送前执行的脚本. 预执行脚本的作用 预执行脚本可以完成以下作用: 编写JS函数等实现复杂计算: 变量的打印 定义.获取.删除.清空环境变量 定义.获取.删除 ...
- 解决 conda tensorflow failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED
参考解决方案1:https://stackoverflow.com/questions/38303974/tensorflow-running-error-with-cublas 参考解决方案2:ht ...
- Linux命令进阶篇之一
利用file命令查看那文件的类型 cd /etc 这里面的文件 命令:file 语法:file [-bLvz] 文件 解释:-b:显示结果,但是不显示文件名称 -L:直接显示符号链接所指向的文件的类型 ...