Several inheritance of the front end

function sort (arr){
var length = arr.length
for (var i =0; i <length - 1; i++){
for (var j= 0 ; j<length- 1 - i; j ++){
if (arr[j] > arr[j +1]){
var temp =arr[j]
arr[j] = arr[j+1];
arr[j+1] =temp ;

}

}
}

return arr

}
Bubbling sort
object-oriented encapsulation inheritance polymorphism
encapsulation
inheritance
1 prototype chain inheritance
function fz () {
this.name = 'gao'
this.son = function () {return this.name + 'ze'}
}
fz.prototype.age = '26 '

function son (){
this.name ='li'
}
son.prototype = new fz();
var sonli = new son();

fz {name: "gao", son: ƒ}
sonli.age//"26"
fz.prototype.age ='260'//"260"
sonli.age//"260"
var son2 =new fz()
son2.age//"260"
sonli.name//"li"
sonli.son()
2. 构造函数继承
function fz(name){
this.name =name
this.son =function (){ return this.name+'ze'}
}
fz.prototype.age ='26'
//可传参
function sonz(){
fz.call(this,'zhang')
}
var sonz=new sonz()
sonz.name //zhang
sonz.age //undefind
sonz.son()//"zhangze"
//可传参
3 组合继承
function fz(name){
this.name =name
this.son =function (){ return this.name+'ze'}
}
fz.prototype.age = '26 '
function sonz (name) {
fz.call (this, name)
}
sonz.prototype = new fz ();
var sub = new sonz (' zhang ')
4 parasitic combination inherited
function fz ( name) {
this.name = name
this.son = function () {return this.name + 'ze'}
}
fz.prototype.age = '26 '
function jsjc (obj) {
function F () {};
F.prototype = obj;
return new F ();
}
var jsZh = jsjc (fz.prototype); // Inherit the prototype of the parent element (property method to use constructor inheritance)
function son (name) {
fz.call (this, name )
}
son.prototype = jsZh;
jsZh.constructor = son; //
var son1 = new son ('lk');
son1.name;
son1.age;

Guess you like

Origin www.cnblogs.com/gaozexiong/p/12710344.html