oop - javascript inheritance for namespace -


i new oop in javascript.

var obj1 = {   name : 'honda',   getname : function(){    console.log(this.name);   } }  var obj2 = {name:'maruti'}; 

i want access getname of obj1 scope of obj2, obj2.getname().

is want:

obj1.getname.call(obj2); 

as @roland said, can use .apply(obj2, [arr of args]) if want provide array of arguments method.

edit: if want import obj1 methods obj2, can pure javascript:

for (var m in obj1) {     // if want copy obj1's attributes, remove line     if (obj1[m] instanceof function) {         obj[2][m] = obj1[m];     } } 

or can jquery:

$.extend(obj2, obj1); 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -