What's the difference between different method declarations in JavaScript? [duplicate] - javascript

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'

Related

What does the colon mean when defining a js function? [duplicate]

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() {
}

Cant understand this anonymous function syntax in Javascript [duplicate]

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

How does the parenthesis at the end of a javascript function supposed to work [duplicate]

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 keyword vs obj name in javascript [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 9 years ago.
Wondering what the difference between the two code below. In the first case I used this to refer to the object and in the second case I used the object name. Although both works I was wondering whether there is any real difference between the two.
(function() {
var app = {
init: function () {
app.addLun('Hello');
},
addLun: function(a) {
console.log(a);
}
};
});
})();
and
var app = {
init: function () {
this.addLun('Hello');
},
addLun: function(a) {
console.log(a);
}
};
});
})();
this refers to the context/scope of the function, so depending on how you call it, it could refer to app, window, or many other scopes...
app refers to the actual app object if it exists in that scope.
Using this and app are absolutely not the same. Take this (slightly contrived) example:
var app = {
init: {
foo: function () {
// Think 'this' is app?
this.addLun('Hello');
}
},
addLun: function(a) {
console.log(a);
}
};
var app2 = {
init: {
foo: function () {
app2.addLun('Hello');
}
},
addLun: function(a) {
console.log(a);
}
};
app2.init.foo(); // prints Hello
app.init.foo(); // error - no method called addLun
this is the current context, app is the object you have just created.
Yes, there is difference. If you want to have more, that one instance of app object (for example, you can clone it with jQuery.extend()), you need to use second variant for correct work.

How to access javascript object methods with a variable? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get property of object in JavaScript
var Terminal = function() {
this.walk = function() {
alert('hello');
}
this.go = 'walk';
this.move = 'walk';
}
var term = new Terminal();
var fn = 'walk';
if (term.hasOwnProperty(fn)) {
term.{fn};
}
How can I run the method term.walk() using the string 'walk'?
There are a couple of ways. The simplest is
term[fn]();
Or alternatively
var funcObj = term[fn];
funcObj.apply(term);
Use term[fn] to access the <fn> property of term.
All properties can be accessed using object["propertyname"]. Globally defined properties/methods can be called through window["propertyname"].
There's only one occasion where variables cannot be accessed through obj["prop_name"]:
function foo(){
var bar = 759;
//There is no "obj" where obj.bar exists.
}

Categories

Resources