attempting to understand the apply function

Y

yawnmoth

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>
 
D

David Mark

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.
 
R

RobG

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(){ ... }
 
T

Thomas 'PointedEars' Lahn

RobG said:
"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top