Confused about function constructor

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
novito
Forum Commoner
Posts: 26
Joined: Mon Apr 07, 2008 11:08 am

Confused about function constructor

Post 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 :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Confused about function constructor

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
novito
Forum Commoner
Posts: 26
Joined: Mon Apr 07, 2008 11:08 am

Re: Confused about function constructor

Post 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 :)
User avatar
novito
Forum Commoner
Posts: 26
Joined: Mon Apr 07, 2008 11:08 am

Re: Confused about function constructor

Post by novito »

A resource related to this, that can be useful....

http://rapd.wordpress.com/2007/12/02/fu ... xpression/
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Confused about function constructor

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Confused about function constructor

Post 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:
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Confused about function constructor

Post 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).
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Confused about function constructor

Post 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
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Confused about function constructor

Post by JellyFish »

Wait, what's not an object in JavaScript, that is, what data-type isn't an object?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Confused about function constructor

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Confused about function constructor

Post 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.
Post Reply