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();

  

posted @ 2018-06-21 19:43  zhanghui_ming  阅读(181)  评论(0编辑  收藏  举报