By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

attempting to understand the apply function

P: n/a
Based on my understanding of the apply function, the following code
snippet should, at the very least, not yield any errors.
Unfortunately, it does, suggesting my understanding is... limited.
Any ideas as to what's wrong?:

<script>
function simpleClass(value) {
this.value = value;
}

(function() {
this.displayValue = function() {
alert("value = " + this.value);
};

this.changeValue = function(value) {
this.value = value;
};
}).apply(simpleClass);

a = new simpleClass("abcd");
a.changeValue("efgh");
a.displayValue();
</script>
Jan 14 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jan 14, 12:04*am, yawnmoth <terra1...@yahoo.comwrote:
Based on my understanding of the apply function, the following code
snippet should, at the very least, not yield any errors.
Unfortunately, it does, suggesting my understanding is... *limited.
Any ideas as to what's wrong?:

<script>
function simpleClass(value) {
* this.value = value;

}
I assume this is meant to be used as a constructor. Use
"SimpleClass" (but that is not the problem.)
>
(function() {
* this.displayValue = function() {
* * alert("value = " + this.value);
* };

* this.changeValue = function(value) {
* * this.value = value;
* };

}).apply(simpleClass);
This is very odd. Also, call could be used as you didn't provide any
parameters to the function. Anyway, all you did was assign two
properties to a Function object. I assume you meant to send the
function's prototype instead.
>
a = new simpleClass("abcd");
Here you created an Object object, which will have a "value" property.
a.changeValue("efgh");
The Object object has no such property.
a.displayValue();
Same.
</script>
Jan 14 '08 #2

P: n/a
On Jan 14, 3:04 pm, yawnmoth <terra1...@yahoo.comwrote:
Based on my understanding of the apply function, the following code
snippet should, at the very least, not yield any errors.
Unfortunately, it does, suggesting my understanding is... limited.
Any ideas as to what's wrong?:

<script>
function simpleClass(value) {
this.value = value;

}

(function() {
this.displayValue = function() {
alert("value = " + this.value);
};

this.changeValue = function(value) {
this.value = value;
};

}).apply(simpleClass);

What you are doing here is calling the anonymous function and setting
its this keyword as a reference to the simpleClass function object.
Therefore, the changeValue property is added to the simpleClass
object.

Try:

alert(typeof simpleClass.changeValue); // --function
a = new simpleClass("abcd");
a.changeValue("efgh");
"a" is a function object constructed from the simpleClass function.
It only has the properties that were added in the constructor and
those that are on the prototype chain. The simpleClass object isn't
on a's prototype chain, so neither is the changeValue property that
you added to simpleClass.

alert(typeof a.changeValue); // --undefined
Change the assignment to:
(function() {
...
}).apply(simpleClass.prototype);
and you'll have more luck. Presumably this is just play, I would not
recommend such obfuscated code for assigning to the prototype
property. Just write it:

simpleClass.prototype.changeValue = function(){ ... }
--
Rob
Jan 14 '08 #3

P: n/a
RobG wrote:
On Jan 14, 3:04 pm, yawnmoth <terra1...@yahoo.comwrote:
>a = new simpleClass("abcd");
a.changeValue("efgh");

"a" is a function object constructed from the simpleClass function.
No, it is a reference to an Object object ...
It only has the properties that were added in the constructor and
.... that is augmented with these properties ...
those that are on the prototype chain.
.... and inherits those.
The simpleClass object isn't on a's prototype chain, [...]
Correct.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Jan 14 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.