473,387 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1379
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
1
by: Christopher Benson-Manica | last post by:
IE 5.0 apparently does not support the apply() method for function objects - the following fails: function foo() { alert( this.length ); } foo.apply( ); Is there a convenient way to add an...
3
by: Blaise Garant | last post by:
Hi I've made a stylesheet to transform my data into XSL-FO. This stylesheet used to work with MSXSL 4.0 but I've got some issues in ..NET. First, I changed removed all the "node-set()" function...
3
by: Studio P.M. | last post by:
Dear colleagues, I kindly ask the C# developers community to help me to REALLY understand the Page_Load event, so to REALLY have it under control. Reason for this question is that I cannot...
3
by: weston | last post by:
I'm making a foray into trying to create custom vertical scrollbars and sliders, and thought I had a basic idea how to do it, but seem to be having some trouble with the implementation. My...
3
by: Jeff Stewart | last post by:
I've been working with the JavaScript Shell in Firefox on a mad-scientist problem I've had in my head. Assume a function named 'object' that is designed to create an object based on a prototype...
3
by: Michael B. Trausch | last post by:
Hello everyone, I am having a problem with static class properties that I suspect is somewhat trivial, but I don't understand what is wrong. I am using GNU C++ on Windows, though I am (hoping)...
2
by: TK | last post by:
HI, I need an example for the usage of the apply()-function. Can you help me? Thanks. o-o Thomas
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.