3

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?
5
  • ball0 is object, not a function. You cann't use operator () on it Commented Sep 9, 2013 at 19:51
  • 3
    Your first comment is completely wrong. You need to learn what the new operator actually does. Commented Sep 9, 2013 at 19:51
  • This may help: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… new does not do what you think it does. It actually creates an instance of an object. Commented Sep 9, 2013 at 19:53
  • 1
    I find it funny when a function named theAdd performs multiplication ;) Commented Sep 9, 2013 at 19:54
  • add3.name sets the name for add3 only. Just that instance. It doesn't modify theAdd at all. Commented Sep 9, 2013 at 19:54

4 Answers 4

3

Because ball0 is an object and you cannot use it like a function

Please check out this to see how new works

Sign up to request clarification or add additional context in comments.

Comments

3
//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:

  1. A new object is created, inheriting from foo.prototype.
  2. The constructor function foo is called with the specified arguments and this bound to the newly created object. new foo is equivalent to new foo(), i.e. if no argument list is specified, foo is called without arguments.
  3. 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:

Comments

1

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:

Why doesn't 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)

alert( Ball() );

This is just a function call, like you would expect, it returns a string which is passed to alert()

alert( ball0() );

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.