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, ...
随机推荐
- SQL 练习36
查询不同课程成绩相同的学生的学生编号.课程编号.学生成绩 select a.cid, a.sid, a.score from sc as a,sc as b WHERE a.sid = b.sid a ...
- mac Charles抓包
手机配置http代理 1.配置iPhone或Android 的wifi配置.首先保证Mac电脑和手机是在同一个局域网内. 2.设置手机wifi配置,在HTTP代理中选择手选代理,服务器填写Mac的IP ...
- 简单介绍无限轮播图,js源代码
无限轮播图js源代码,今天介绍一下用js简单的编写无限轮播图 <!DOCTYPE html> <html> <head> <meta charse ...
- 无法加载文件 E:\PROGRAM FILES\NODEJS\NODE_GLOBAL\yarn.ps1,因为在此系统中禁止执行脚本
参考: npm : 无法加载文件 C:\Program Files\nodejs\node_global\npm.ps1,因为在此系统上禁止运行脚本.
- COM笔记-引用计数
参考网站:https://www.cnblogs.com/fangyukuan/archive/2010/06/06/1752621.html com组件将维护一个称作是引用计数的数值.当客户从组件取 ...
- .net core2.1 迁移.net core 3.1
1.解决方案->属性-->目标框架 .net core3.1 2.删除旧的Nuget包添加新的NuGet包 3.修改Startup.cs 修改ConfigureServices 修改Con ...
- 【开发工具】idea常用配置
1. 设置鼠标滚轮修改字体大小 我们可以勾选此设置后,增加Ctrl + 鼠标滚轮 快捷键来控制代码字体大小显示. 2. 设置鼠标悬浮提示 3. 设置自动导包功能 Add unambiguous im ...
- 搭建zabbix监控系统详解
搭建zabbix监控系统详解 文:warren 博文大纲:一.前言 二.zabbix监控架构三.搭建Zabbix监控服务器四.搭建过程中遇到有些服务无法正常启动的解决办法 一.前言 : 要想实时的 ...
- jQuery中的常用方法:empty()、each()、$.each()、$.trim()、focus()(二)
<!DOCTYPE html> <html> <head> <title>02_commonMethod.html</title> < ...
- c++制表符
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 //制表符是8个字符,当在一个制表符开始的位置(明白什么位置是 ...