/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Adding methods with prototype

I pulled out an old javascript object I created a while back a few days ago and I’ve been developing in Firefox ever since. Then I decided to test how it worked in other browsers and…..it works in none of them! Not chrome, not IE(7/8), not opera, and not safari.

It’s built like this:

[code]
function myCalendar() {
//constructor function
}

myDiv.prototype.newMethod = function() {
//add another method to objects created through myDiv
}

//do some stuff onload
myObj = new myCalendar();
myObj.newMethod();
[/code]

Every browser (except firefox) breaks at this line:

[code]
myObj.newMethod();
[/code]

It’s really as simple as that….what’s on the go? Do any JS experts out there know anything about this?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscauthorMar 19.2010 — To add further misery and proof that I must be going notes, I've changed it up a little and saw no difference:

<i>
</i>function myCalendar() {
//constructor function
this.newMethod = function() {
//adds a method to an object created by myCalendar
}
}

//do some stuff onload
myObj = new myCalendar();
myObj.newMethod();
Copy linkTweet thisAlerts:
@aj_nscauthorMar 19.2010 — Okay, nevermind, I figured it out. Apparently, all these browsers handle uninitiated objects differently.

For the record, I called a function with a variable number of arguments to instantiate an object:

<i>
</i>function makeObjs() {
argumentsArray = new Array();
for(i=0;i&lt;arguments.length;i++) {
if(window[arguments[i]]) {
//deal with the object that has already been created
} else {
//create a new object
window[arguments[i]] = new Object();
}
}
}


The problem was that all browsers, other than firefox, identified window[arguments[i]] as an object whereas firefox identified it as undefined until the object was created with window[arguments[i]].



I worked around it by testing to see if a property of the object was set and if the object was defined.



<i>
</i>function makeObjs() {
argumentsArray = new Array();
for(i=0;i&lt;arguments.length;i++) {
if(window[arguments[i]] &amp;&amp; window[arguments[i]].someproperty !== undefined) {
//deal with the object that has already been created
} else {
//create a new object
window[arguments[i]] = new Object();
}
}
}


And that made it work cross browser.....
Copy linkTweet thisAlerts:
@JMRKERMar 19.2010 — I understand that the problem is resolved to your satisfaction,

but could you explain a little further;

1. How would I use this? Any concrete examples?

2. When would I use this, as opposed to some other possible code?

3. Why would I use this? Is there an advantage that I'm not seeing?

I guess the bottom line is, this looks interesting, but I'm not sure how or why to implement it!

Do you have some examples of its use I could study upon and ask questions about?
×

Success!

Help @aj_nsc 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.6,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...