对象的继承
function MyArray ( ) { }
MyArray. prototype = new Array ( ) ;
MyArray. prototype. constructor = MyArray;
var mine = new MyArray ( ) ;
mine. push ( 1 , 2 , 3 ) ;
console. log ( mine. length)
mine instanceof Array ;
function P ( ) { }
var p = new P ( ) ;
console. log ( p. constructor === P ) ;
console. log ( p. constructor === P . prototype. constructor)
p. hasOwnProperty ( "constructor" )
function Constr ( ) { }
var x = new Constr ( ) ;
var y = new x. constructor ( ) ;
y instanceof Constr
Constr. prototype. createCopy = function ( ) {
return new this. constructor ( ) ;
} ;
var b = x. createCopy ( ) ;
function Person ( name) {
this . name = name;
}
Person. prototype. constructor === Person
Person. prototype = {
method: function ( ) { }
} ;
Person. prototype. constructor === Person
Person. prototype. constructor === Object
function Person ( name, age) {
if ( this instanceof Person ) {
this . name = name;
this . age = age;
} else {
return new Person ( name, age) ;
}
}
function Father ( name, age, height, weight) {
this . name = name;
this . age = age;
this . height = height;
this . weight = weight;
}
function Son ( name, age, height, weight) {
Father. call ( this , "huzhixin" , 19 , 170 , 130 ) ;
}
var father = new Father ( ) ;
var son = new Son ( )
function Super ( ) { } ;
Super. prototype. name = "abc" ;
Super. prototype. age = 20 ;
function Sub ( ) { }
Sub. prototype = Object. create ( Super. prototype) ;
Sub. prototype. constructor = Sub;
var sub = new Sub ( ) ;
function M1 ( ) {
this . hello = "hello" ;
}
function M2 ( ) {
this . world = "world" ;
}
function Mine ( ) {
M1 . call ( this ) ;
M2 . call ( this ) ;
}
var mine = new Mine ( ) ;
Mine. prototype = Object. create ( M1 . prototype) ;
Object. assign ( Mine. prototype, M2 . prototype) ;
Mine. prototype. constructor = Mine;
console. log ( mine) ;
console. log ( Mine. prototype. constructor)