Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a function in javascript object literal notation declaration

I'm trying to call a function within an object literal that I created, using the this keyword. But an error shows up saying this.doTheMove() is not a function:

window.onload = function(){

  var animBtn = document.getElementById('startAnim');

  animBtn.addEventListener('click', Animation.init, false);

}

var Animation = {
  init: function(){

     this.doTheMove(); // I'm calling the function here, but it gives an error.

  },
  doTheMove: function(){

    alert('Animation!');

  }
}

Why is there an error?

like image 203
Shaoz Avatar asked Jan 27 '11 18:01

Shaoz


People also ask

How do you call a function inside an object in JavaScript?

The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.

What is object literal notation in JavaScript?

The Object literal notation is basically an array of key:value pairs, with a colon separating the keys and values, and a comma after every key:value pair, except for the last, just like a regular array. Values created with anonymous functions are methods of your object. Simple values are properties.

How do you declare an object literal?

Create Object using Object Literal SyntaxDefine an object in the { } brackets with key:value pairs separated by a comma. The key would be the name of the property and the value will be a literal value or a function. Syntax: var <object-name> = { key1: value1, key2: value2,...};

How do you declare a function in JavaScript?

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...)


3 Answers

An explanation of what's happening. Pointy's answer is good but I want to explain it more generically. A very good research on this can be found here

An event handler is just a callback. You pass it a function and an event to listen on. Interally all it will do is call that function.

Animation.init is just a getter for that function. Think of it like this:

var callback = Animation.init
animBtn.addEventListener('click', callback, false);
...
// internal browser event handler
handler() {
   // internal handler does stuff
   ...
   // Oh click event happened. Let's call that callback
   callback();
}

So all you've done is passed in

var callback = function(){
   this.doTheMove(); // I'm calling the function here, but it gives an error.
}

By default in javascript this === window. This will refer to the global object if it isn't set to something. The net effect is that window.doTheMove is called. And that function doesn't exist.

In this case since callback is actaully called by an event handler the this object points at the DOM object that triggered the event so your calling node.doTheMove which still doesn't exist.

What you wanted to do is wrap it with a reference to Animation.

var callback = function() {
    Animation.init();
}

This is a function execution and it executes init on Animation. When you execute it on an object like that then internally this === Animation as you would expect.

To sum up. The issue here is that Animation.init is just a reference to a function. It has no information about anything else like Pointy mentioned.

like image 84
Raynos Avatar answered Oct 18 '22 21:10

Raynos


You have to change the way you set that up:

window.onload = function(){

  var animBtn = document.getElementById('startAnim');

  animBtn.addEventListener('click', function() { Animation.init(); }, false);

}

In JavaScript, the fact that a function happens to be defined as part of an object literal really doesn't mean very much (if anything, in fact). The reference to Animation.init does get you to the proper function, but the problem is that when the function is later invoked (in response to an actual "click"), the browser calls the function but has no idea that the object "Animation" should be the this reference. Again, the fact that the function was declared as part of the object is of no importance at all here. Therefore, if you want this to be something in particular of your own choosing, then you have to make sure it's set explicitly in code you control. The solution above is about the simplest way to do it: it handles the "click" events with an anonymous function that does nothing other than invoke the "init" function via an explicit reference through "Animation". That will ensure that this refers to the "Animation" object when "init" runs.

Another alternative would be to use the ".bind()" facility that some browsers and frameworks support:

window.onload = function(){

  var animBtn = document.getElementById('startAnim');

  animBtn.addEventListener('click', Animation.init.bind(Animation); }, false);

}

The net effect is almost exactly the same: that call to ".bind()" returns a function that invokes the function on which it was called (that being the "init" function in the "Animation" object), and does so with its first argument as the this reference (the "context" object). That's the same thing that we get from the first example, or effectively the same anyway.

like image 29
Pointy Avatar answered Oct 18 '22 19:10

Pointy


Here's another nice approach, I think.

window.onload = function(){

  var animBtn = document.getElementById('startAnim');

  animBtn.addEventListener('click', Animation.init, false);

};

var Animation = {
    init: function(){

        Animation.doTheMove(); // This will work, but your IDE may complain...

    },
    doTheMove: function(){

        alert('Animation!');

    }
};
like image 20
StephenKC Avatar answered Oct 18 '22 20:10

StephenKC