Home  >  Article  >  Web Front-end  >  Detailed explanation of js mixed inheritance examples

Detailed explanation of js mixed inheritance examples

小云云
小云云Original
2018-03-13 16:31:191545browse


1. Application scenario: Suppose there are two objects o1 and o2. If you want to copy the methods and properties of o2 to o1, mixed inheritance is one of the simplest methods
2. Implementation method: Use for...in... to traverse the properties and methods of the o2 object, and assign the properties and methods of o2 to o1

Code implementation

function mixExtend( o1, o2 ){
    for( var key in o2 ){
        o1[key] = o2[key];
    }    return o1;
}


----------//调用一下var o1 ={};

mixExtend( o1, { name:"二狗",age:18,say:function(){
    console.log("大家好,我是二狗");
} });

console.log(o1);

Related recommendations:

Parasitic inheritance of JS inheritance

Sharing of several js inheritance styles

##js Inherited implementation code_javascript skills

The above is the detailed content of Detailed explanation of js mixed inheritance examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn