I want to create a method in javascript which can be used by every object which is an element. no need to add the method like
object = {
methodName : function(){
}
};
I want to create a method in javascript which can be used by every object which is an element. no need to add the method like
object = {
methodName : function(){
}
};
Your best bet is to extend Object
prototype.
// extend
Object.prototype.myFunction = function(){
console.log('universal Object function');
}
// now every object created and instance of Object will have the method myFunction available to use.
var a = {};
a.myFunction();
Please, bear in mind this Why is extending native objects a bad practice?