Page 1 of 1

Confused about function constructor

Posted: Thu Aug 12, 2010 2:14 pm
by novito
Hi there,

I've been reading a tutorial, and at one point, a method is declared like this:

this.addBook = new
Function("book","this.books.push(book)");

What this does is adding a book into the books array that that class has. However, I am more used to do it like:

this.addBook = new Function(book){
this.books.push(book);}.

So, my question is: Can a function parameter interact with the other parameters? As in this case, the second parameter is using the first parameter. Also, why the parameters are passed with " ", if book is an object (of the class Book) and not a string?

Thank you :)

Re: Confused about function constructor

Posted: Thu Aug 12, 2010 2:34 pm
by pickle
No doubt - that code is weird. What's actually being passed are 2 strings. Likely the second argument is being run through eval() in order to be executed.

Parameters cannot interact with other parameter's per se. They can act on variables that are passed as other parameters, but not on the value in the parameter. For example, if "myNumber" was passed as the first argument, the second argument can be a function call that uses myNumber - Math.floor() for instance. However, if the first argument calls Math.floor() on myNumber, there's no way to use that result in the second argument.

In short, that code is crap. The way you're used to doing it is much better.

Re: Confused about function constructor

Posted: Thu Aug 12, 2010 3:21 pm
by novito
:)

Thanks for the answer! Looking it up online I found out that while whatever = function() is a function literal, when you do whatever = new function() is a function constructor. You have to pass the parameters plus the body of the function.

As you say, from my ignorance, it seems crappy :)

Re: Confused about function constructor

Posted: Thu Aug 12, 2010 3:23 pm
by novito
A resource related to this, that can be useful....

http://rapd.wordpress.com/2007/12/02/fu ... xpression/

Re: Confused about function constructor

Posted: Thu Aug 12, 2010 3:25 pm
by pickle
novito wrote::)

Thanks for the answer! Looking it up online I found out that while whatever = function() is a function literal, when you do whatever = new function() is a function constructor. You have to pass the parameters plus the body of the function.

As you say, from my ignorance, it seems crappy :)
Well, I guess we were both ignorant about how function constructors work. I had no idea that was part of the language itself.

Re: Confused about function constructor

Posted: Sun Aug 22, 2010 9:30 pm
by JellyFish
Right, it is crappy. It's just as crappy as doing this:

Code: Select all

var num = new Number(23);
instead of this:

Code: Select all

var num = 23;
Why use a constructor function to create an object when it's already natively built-in the language? Sense everything in JavaScript is an object, there is no difference in expressing it literally or with a constructor function.

I think the real use in constructor functions is when you're looking to extend objects with a custom "class" (I put class in quotes because JavaScript doesn't really have classes).

Just my two cents. :wink:

Re: Confused about function constructor

Posted: Mon Aug 23, 2010 4:37 am
by Weirdan
JellyFish wrote: Sense everything in JavaScript is an object, there is no difference in expressing it literally or with a constructor function.
Not everything in JavaScript is an object, and there's is a difference (mainly in forms of various gotchas).

Re: Confused about function constructor

Posted: Mon Aug 23, 2010 9:27 pm
by PHPHorizons
Weirdan wrote:Not everything in JavaScript is an object
True, but I think the statement that everything in JavaScript is an object is 99.99% correct. Every number, and every string is an object. You can't have a variable that contains a string or a number that isn't an object.

Cheers

Re: Confused about function constructor

Posted: Mon Aug 23, 2010 10:40 pm
by JellyFish
Wait, what's not an object in JavaScript, that is, what data-type isn't an object?

Re: Confused about function constructor

Posted: Tue Aug 24, 2010 2:33 am
by Weirdan
PHPHorizons wrote:
Weirdan wrote:Not everything in JavaScript is an object
Every number, and every string is an object. You can't have a variable that contains a string or a number that isn't an object.
Nope, that's not true. Granted, you may call methods on primitive values like strings and numbers, but when you do so, they are autoconverted to objects, they are not objects to start from. Once the method call (or any other property access) is over this temporary object is thrown away. One of the consequences of this autoconversion is that primitive "objects" do not keep values of properties you create on them:

Code: Select all

> var a = "qwe";
> typeof a
"string"
> var b = new String("qwe");
> typeof b;
"object"
> a.prop = "val";
"val"
> a.prop
undefined // <===== it doesn't keep the property
> b.prop = "val";
"val"
> b.prop 
"val" // <==== unlike real object

Re: Confused about function constructor

Posted: Tue Aug 24, 2010 2:43 am
by Weirdan
JellyFish wrote:Wait, what's not an object in JavaScript, that is, what data-type isn't an object?
ES5 spec wrote: The ECMAScript language types are Undefined, Null, Boolean, String, Number, and Object.
So, anything that is not an object in the above list. Boolean, String and Number have their object counterparts though.