Reputation: 550
I stunbled on this through stackoverflow and it is a great resource and there is alot of conversation on this but i'm just still baffled why certain things dont work?
I studied c++ in college and sorta remember pointers
http://www.permadi.com/tutorial/jsFunc/index2.html
my problems are started in the code certain things aren't working
function theAdd(a, b){
return a*b;
}
//creating a copy of the function
var add3 = new theAdd();
add3.name = 'nameProperty';
//alert( add3(1,2)) //why doesn't this work?
alert(theAdd(5,5) + " " + theAdd.name);
function Ball() {
return 'balltext';
}
var ball0=new Ball(); // ball0 now points to a new object
alert( Ball() ); // this works
alert( ball0() ); // why doesn't this work? shouldn't it return balltext?
Upvotes: 3
Views: 102
Reputation: 626
The line var ball0 = new Ball();
is invoking the Ball method which returns a value of 'balltext', so the variable ball0 now has a value of 'balltext' which is a string, not a function.
Your last line of code alert(ball0());
is attempting to call a function, which would work if ball0 was a reference to a function. Since the value of ball0 is a string this won't work. To pop open an alert that prints the value of the variable ball0 your code should read alert(ball0);
Hope this helps.
Upvotes: 1
Reputation: 816404
//creating a copy of the function var add3 = new theAdd();
This does not create a copy of the function. It calls the function as constructor function. add3
will be the object that was created by that function.
alert( add3(1,2))
why doesn't this work?
Because add3
is not a function (it's a normal object).
alert( Ball() );
this works
You are calling the function the "normal" way. The result of the invocation will be the value the function returns.
alert( ball0() );
why doesn't this work?
ball0
is not a function.
shouldn't it return balltext?
No. The new
keyword invokes the function in a special way. From the MDN documentation:
When the code new foo(...) is executed, the following things happen:
- A new object is created, inheriting from
foo.prototype
.- The constructor function
foo
is called with the specified arguments and this bound to the newly created object.new foo
is equivalent tonew foo()
, i.e. if no argument list is specified, foo is called without arguments.- The object returned by the constructor function becomes the result of the whole new expression. If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.)
Since you are not returning an object but a primitive value (a string in this case), the object that was created in the first step is returned and assigned to ball0
.
When you are calling a function with new
then your are using the function as constructor function, i.e. it is supposed to create a new object. The new
operator ensures that an object will always be returned, no matter what the function itself returns.
Also have a look at:
Upvotes: 3
Reputation: 560
Quite a few questions there, but a lot of it relates to the concept of objects in JavaScript, and constructor functions. When you use the new
operator, it creates an 'empty' object, and then calls the function with the object passed as the this
parameter. Any function can be used as a constructor which I think is causing some of the confusion.
Going through some of the questions:
add3(1,2)
add the numbers?This is because add3
is an object, not a function, you used the new operator (and in this case, the constructor didn't do anything with this
, its return value was ignored at construction time)
This is just a function call, like you would expect, it returns a string which is passed to alert()
This doesn't do what you expect because ball0 is an object, using Ball as its constructor
Heres a pretty good primer for JavaScript objects over at W3Schools
Upvotes: 1
Reputation: 172428
Because ball0
is an object and you cannot use it like a function
Please check out this to see how new works
Upvotes: 3