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

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -