I've been trying to see if there's a way around the typical process of extending an object, whereby it shows that it's assigning the extended properties to prototype of the object, but attached to the 'extend' method itself on the current object. I want the properties to be assigned DIRECTLY on the current object (ie. "this.property" and not "this.extend.property"). Let me explain:
As far as I know, there are two ways to "extend" an object in Javascript:
1) global function:
function extend(a, b){
for(var key in b)
if(b.hasOwnProperty(key))
a[key] = b[key];
return a;
}
I can then run this code to "extend" my App object:
function App() {
extend(this, {
command: function () {
alert('command!');
}
});
console.log(this);
return this;
}
2) Another way to extend is to use the Object prototype directly, ie:
Object.prototype.extend = function(obj) {
for(i in obj)
this[i] = obj[i];
};
And then extend App with:
function App() {
this.extend({
command: function () {
alert('command!');
}
});
console.log(this);
return this;
}
However, while the 'command' function will now be accessible on the App object in either case above, both of them still show that the command function is an extension of the 'extend' method in the App prototype. The console output from both of these shows: (hiding other App properties for brevity):
command: function () {
arguments: null,
caller: null,
length: 0,
name: "",
prototype: App.extend.command
}
Notice the 'App.extend.command'? I am wondering if there is ANY way to extend the properties so that they are DIRECT attributes of the App object, so that prototype will show: "prototype: App.command".
I can achieve this if I assign the propertie directly to "this", like:
function App() {
this.command = function () {
alert('command!');
}
console.log(this);
return this;
}
Which outputs:
command: function () {
arguments: null,
caller: null,
length: 0,
name: "",
prototype: App.command
}
However, I don't want to have to use the format of "this.blah = function()" for all of my properties. I would rather just extend the current object with a JSON list of properties as shown in the first two examples. Is there anyway to do this so that I can still keep my 'newable' function declarations?
I will add that is preferable to NOT use the prototype method, as this will add the 'extend' property to ALL objects in the application, which is not desirable for certain objects.
Thanks for reading!