* 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. STM32—中断详解(配合按键中断代码,代码亲测)

    在STM32中执行中断主要分三部分: 1.配置NVIC_Config()函数 2.配置EXTI_Config()函数 3.编写中断服务函数 (注:本文章所用代码为中断按键代码,实现了按键进入中断从而控 ...

  2. 【Azure 应用服务】App Service .NET Core项目在Program.cs中自定义添加的logger.LogInformation,部署到App Service上后日志不显示Log Stream中的问题

    问题描述 在.Net Core 5.0 项目中,添加 Microsoft.Extensions.Logging.AzureAppServices 和 Microsoft.Extensions.Logg ...

  3. Python使用flask架构、跨域

    from flask import Flask import json from flask_cors import CORS Server = Flask(__name__) cors = CORS ...

  4. QT中的对象模型――QPointer

    QPointer是一个模板类,为QObject对象提供了守卫指针(Guarded Pointer).什么是守卫指针?守卫指针QPointer<T>类似于普通C++指针T *,有且仅有一点不 ...

  5. 刷题-力扣-1738. 找出第 K 大的异或坐标值

    1738. 找出第 K 大的异或坐标值 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/find-kth-largest-xor-co ...

  6. 高德地图——步行路线&步行路线的坐标规划

    步行操作与开车一样 唯一区别就是src末尾加入&plugin=AMap.Walkling 以及new AMap.Walking({}) <!DOCTYPE html> <ht ...

  7. Mysql时间戳转Java时间戳

    MySQL 时间戳和Java返回的时间戳是不一样的 例如: 当前时间是 2014-08-04 10:42:55.204000 使用mysql时间戳函数UNIX_TIMESTAMP 返回的结果为: 14 ...

  8. TCP/IP以及Socket聊天室带类库源码分享

    TCP/IP以及Socket聊天室带类库源码分享 最近遇到个设备,需要去和客户的软件做一个网络通信交互,一般的我们的上位机都是作为客户端来和设备通信的,这次要作为服务端来监听客户端,在这个背景下,我查 ...

  9. (四)羽夏看C语言——循环与跳转

    写在前面   由于此系列是本人一个字一个字码出来的,包括示例和实验截图.本人非计算机专业,可能对本教程涉及的事物没有了解的足够深入,如有错误,欢迎批评指正. 如有好的建议,欢迎反馈.码字不易,如果本篇 ...

  10. 对 RESTful 的理解

    REST 全称 Representation State Transfor (资源表现层状态改变) 实际上是指客户端通过http/https协议手段来改变URI的状态转化,达到请求不同的资源的目的. ...