This question already has answers here:
What does the colon (:) in JavaScript represent?
(2 answers)
Closed 6 years ago.
I'm just learning to make JavaScript games using Phaser and have a question.
I don't understand what the colon means when defining a function. For example:
attackEnemy: function(player, ground) {
}
And how is it different to doing it like this...
function attackEnemy() {
}
When you are using attackEnemy:, you are not doing it outside. You should be doing it inside an object, say:
var game = {
attackEnemy: function(player, ground) {
},
otherProperty: value
};
An object can have function as a member as well. This is how you define functions in the objects.
You can use colon syntax when defining a key for object, like this:
var object = {
attackEnemy: function(player, ground) {
}
}
When you want define a function as separate element, you can do that (Function Declaration):
function attackEnemy() {
}
or that (Function Expression):
var attackEnemy = function() {
}
Related
This question already has answers here:
What does arrow function '() => {}' mean in Javascript? [duplicate]
(2 answers)
Closed 7 years ago.
The following code:
angular.module('socially').controller('PartiesListCtrl', function ($scope)
{
$scope.helpers({
parties: () => {
return Parties.find({});
}
});
});
demo at Angular Meteor Tutorial
Can't understand the syntax used for parties: object. Why is => used ? Is there more explanation for this kind of anonymous function.
This is an arrow function, new syntax from ES2015 standard which was accepted this year. Not only arrow functions are shorter in declaration and sometimes looks nicer, they also share binding context with their surrounding code
!function() {
this.name = 'global';
var nonArrowFunc = function() {
console.log(this.name); // undefined, because this is bind to nonArrowFunc
}
var arrowFunc = () => {
console.log(this.name); // this taken from outer scope
}
}();
You can read more about arrow functions here and here and here
This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 8 years ago.
I have seen developers using different ways to declare/define functions in js,
like:
// 1
createview:function()
{
}
// 2.
var createview=function()
{
}
// 3.
function createview()
{
}
While 2nd and 3rd are function expression and declaration respectively, what's with the 1st usage?
Its an object and used for example in the revealing module pattern.
var module = (function() {
var stuff = function() { return 'stuff'; };
return {
stuff: stuff
}
}());
Now you have a name spaced closure.
module.stuff() //--> returns 'stuff'
This question already has answers here:
What do parentheses surrounding an object/function/class declaration mean? [duplicate]
(7 answers)
Closed 8 years ago.
I consider myself a pretty strong javascript coder and am familiar with most all of the javascript syntax. But have been puzzled by the following syntax:
function() {
return function() {
}
} ();
Can someone explain what the parenthesis at the end is supposed to be used for?
So, the expression:
(function() {
return function() {
}
})
Evaluates to a function (without a name in this case) that returns some other function.
Adding ():
(function() {
return function() {
}
})();
Would simply call that function.
Another way to write this would be:
var foo = function() {
return function() {
}
};
foo();
It is a self invoking function. Meaning a function that declares and calls itself.
Another form would be:
(function() {
return function() {
}
}());
This question already has answers here:
how do I compare 2 functions in javascript
(6 answers)
Closed 6 years ago.
I need to detect whether two anonymous functions are the same.
In the console, why does the following line return false?
(function() { alert("hello"); }) === (function() { alert("hello"); })
Is there a comparison operator or other method to identify that these are 'functionally' the same?
EDIT:
People are asking for the use of this.
It's for removing a function that has been previously pushed into an array of functions. See http://jsfiddle.net/w6s7J/ for a simplified test.
There is no real way to compare 2 different anonymous functions for 'functionality'.
You can check if they are the same object by using your example code.
var func = function () {
alert('hello');
};
alert(func === func);
The above will work, as you are checking to see that both objects are the same.
The only other method for comparing is to compare them as a string.
var func1 = function () {
alert("hello");
};
var func2 = function () {
alert('hello');
};
alert(func1.toString() === func2.toString());
Oops!, they are functionally the same, the difference is the quotes used, So this returns false.
(function() { alert("hello"); }) === (function() { alert("hello"); })
Returns false because they are different objects in javascript.
(function() { alert("hello"); }).toString() === (function() { alert("hello"); }).toString()
Returns true because they are the same strings.
Also function objects have a name property which return the name of the function (but it does not work in IE):
var a = function b() {}
alert(a.name);//write b
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What do parentheses surrounding a JavaScript object/function/class declaration mean?
I have found the following code in a website .
var testModule = (function(){
var counter = 0;
return {
incrementCounter: function() {
return counter++;
},
resetCounter: function() {
console.log('counter value prior to reset:' + counter);
counter = 0;
}
};
})();
So it follows the syntax var a = (blah balh..)()
What does it actually mean? What is the meaning of variable declaration like a =()()..
It's defining a single-use function and executing it immediately. The code you provided is named the Module Pattern -- see here for more information about its properties: http://www.yuiblog.com/blog/2007/06/12/module-pattern/
A normal function might be created like this:
var f1 = function() {
console.log('bar');
};
And you could subsequently call it like so:
f1();
But in the example you provided, the function is both defined and executed once, and that function returns an object with two functions: incrementCounter and resetCounter. You can call them like so: testModule.incrementCounter() and testModule.resetCounter()
The Module Pattern is useful when you have a single object and you want to encapsulate some properties which are only available to the functions defined in the closure.
The anonymous function is executed and the return value is assigned to the variable.