470,630 Members | 1,729 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,630 developers. It's quick & easy.

attempting to understand the apply function

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
3 1290
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
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
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.

Similar topics

1 post views Thread by Christopher Benson-Manica | last post: by
3 posts views Thread by Blaise Garant | last post: by
3 posts views Thread by Studio P.M. | last post: by
3 posts views Thread by Jeff Stewart | last post: by
3 posts views Thread by Michael B. Trausch | last post: by
2 posts views Thread by TK | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.