/    Sign up×
Community /Pin to ProfileBookmark

Add method to all objects

Alright well I know that I can add methods to objects and what not but is there a way I can add a method to all objects or possibly an object superclass?

Cause I would like to be able to do something like this

All elements on the page now are able to use methodA by going like this

results = element.methodA();
results2 = elementQ.methodA();

Without having to go through and add the method to each element.

Will just doing document.prototype work since its the super class of all the other elements. Or would I have to go about it some other way? I ask if document.prototype would work because I’m at school and cant really test it out here

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@voidvectorFeb 03.2009 — Note that both methods can easily break existing code or libraries using [COLOR="Red"]for (.. in ..)[/COLOR].

1) For IE8 and current versions of most other browsers, you can access the Element prototype.
<i>
</i>Element.prototype.methodA = function () { ... }


2) For all browsers, you can overload the Object prototype itself. This method is very prone to the above mentioned problem.
<i>
</i>Object.prototype.methodA = function () { ... }
Copy linkTweet thisAlerts:
@toicontienFeb 03.2009 — By [B]element[/B] do you mean a Document Object Model node? If so, I don't believe the JavaScript currently supported by browsers allows you to do this. You can add methods to ALL objects:
[CODE]Object.prototype.methodA = function() {
// Code here
};[/CODE]


Then EVERY object has this method, including arrays, functions, DOM nodes, including strings, numbers and boolean variables when used as objects (e.g. var total = 0; [B]total.toString()[/B]).
Copy linkTweet thisAlerts:
@posiden5665authorFeb 03.2009 — Ah ok thank you toi I meant to say Object.prototype up there not document.prototype thank you and now I can test it out when i get home to try and get stuff working thank you
Copy linkTweet thisAlerts:
@posiden5665authorFeb 03.2009 — Actually I am having problems with IE and the Object.prototype

So i am trying to do this:
function methodA(classN, tagType)


and add it to all objects using this
Object.prototype.getMethodA = methodA

And it works perfectly fine in FF but not IE its failing and saying the object doesnt have that method.
Copy linkTweet thisAlerts:
@toicontienFeb 03.2009 — It would be easier to use do:
[CODE]Object.prototype.getMethodA = function() {
// Code here
};[/CODE]

That way you aren't cluttering up the global name space with unnecessary functions.

From what you posted, there aren't any errors. Can you please post the [I]full[/I] JavaScript code you are using to extend the Object class? The problem must be elsewhere.
Copy linkTweet thisAlerts:
@posiden5665authorFeb 03.2009 — I do it that way because I like it that way and I can

Anyway

It doesnt matter what I put i could put something as simple as

Object.prototype.testing = function(){
alert("hello");
};


And whenever I try and run it IE says its not a property or method.
Copy linkTweet thisAlerts:
@posiden5665authorFeb 03.2009 — Ok so I found out that it is setting it in IE for Object.testing() and I can do that fine but if i wanna do something like document.testing() it wont work and says its not there
Copy linkTweet thisAlerts:
@rnd_meFeb 04.2009 — Ok so I found out that it is setting it in IE for Object.testing() and I can do that fine but if i wanna do something like document.testing() it wont work and says its not there[/QUOTE]

it works in firefox.

IE doesn't like to share certain native objects like document, activeX, etc.
Copy linkTweet thisAlerts:
@posiden5665authorFeb 04.2009 — Anywork around cause I need to be able to add it to document and crap I'd like to be able to do things like so:


test = document.addedMethod("argument")

hello = test.addedMethod("argument")



Just like you can do with this:

crap = document.getElementById("id")

hello = crap.getElementById("id2")
Copy linkTweet thisAlerts:
@rnd_meFeb 04.2009 — Anywork around cause I need to be able to add it to document and crap I'd like to be able to do things like so:

[/QUOTE]


you can create a custom object upon the first use, and bind your methods to that custom object.

it's a bit complicated, but it works well, and won't interfere with outside code like .prototype will.

jQuery is probably the most famous example of this approach.
Copy linkTweet thisAlerts:
@posiden5665authorFeb 04.2009 — Damn alright well thanks for the help
×

Success!

Help @posiden5665 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...