This question already has answers here:
Why does CoffeeScript wrap class definitions in a closure?
(4 answers)
Closed 8 years ago.
CoffeScript compiles this:
class A
a: 'value'
to:
var A;
A = (function() {
function A() {}
A.prototype.a = 'value';
return A;
})();
What is the difference with this:
var A = function A(){};
A.prototype.a = 'value';
I tested the codes in console and the first returns function A(), while the second returns "value", but as a class is intended to be instantiated, to use class A, myA = new A() works for both cases.
There's no effective difference, but since CoffeeScript is a code generator, it likely has other uses for the variable scope in different situations, and is simply not optimized to reduce the code for the simple situations that don't actually need the extra scope.
I don't use CoffeeScript, but that would be my guess.
Related
This question already has answers here:
encapsulation in javascript module pattern
(1 answer)
JavaScript Encapsulation
(7 answers)
Preferred way of encapsulating Javascript and defining private variables/methods?
(2 answers)
Closed 5 years ago.
I am tasked with refactoring some code from a former employee. He uses a constructor function that has two things that seem strange – 1, arbitrary code that runs outside of a function or property, and 2, locally defined functions. This is a simplification of what he is doing:
var Dog = function(){
// Arbitrary code
console.log('I am a dog');
var foo = 'foo';
// Function defined to local variable
var bar = function(){
console.log(foo);
console.log('bar');
};
// Normal function in a constructor
this.bark = function(){
console.log('bark');
};
};
var d = new Dog();
Is there any merit to this style of constructor function? Or would it be better to refactor it to only define functions using this in the style of bark, and then running them on d as needed?
Well I don't think there is any special 'merit' is just one in many ways of doing things. The code is simply run on instantiation.
The 'bar' function is hidden from the class user (or 'private' if you like the old OO name), and the 'bark' function can be called from outside the class.
It's one of the ways of doing some sort of OO in JS. There are plenty.
Javascript is a very 'free' language. You see people using different ways of doing things all the time. There is not only one 'right' way. Get used to it :)
This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 7 years ago.
Is there ANY difference in the following two ways of defining a functions?
METHOD 1)
var printName = function(name){
return("Hi! My name is ",name)
}
VS
METHOD 2)
function printName(name){
return("Hi! My name is ",name)
}
and I mean ANY, I'm new at JS and want to lay down my understanding of functions and Objects before I advance as I feel these 2 features are the 2 I'll use the most.
Yes there is a difference, but none that would affect the performance of the function code when it's called.
The difference has to do with when the function is created, but the performance is identical. Using your examples:
printName_1("Drew"); // This will fail, as printName_1 is not defined (yet)
printName_2("user4820485"); // This will work
var printName_1 = function(name){
return "Hi! My name is "+name;
}
function printName_2(name){
return "Hi! My name is "+name;
}
Functions that are declared using the latter syntax are initialized at the beginning of the block where they appear, so it looks like they can be called before they are defined.
This question already has answers here:
Why JavaScript function declaration (and expression)?
(5 answers)
Closed 8 years ago.
I'm assigning a named function expression to a property of an object:
var foo = {};
foo.bar = function bar(){
console.log('bar');
};
foo.bar() => bar
bar() => ReferenceError: bar is not defined
Why can't I call bar() directly, why isn't it defined?
I know that I can simply chain the assignment like var bar = foo.bar = function(){}, so I'm not looking for a work-around or other solution, I'm only interested in why it doesn't work.
I've tested in Chrome console and Node.JS.
Named function expressions are simply not supposed to work that way. The function name is only visible inside the function, not outside.
A function instantiation expression that happens to have a name is not the same thing as a function declaration statement. Those are two distinct syntactic (and semantic) entities in the language.
This question already has answers here:
How do JavaScript closures work?
(86 answers)
Why is this function wrapped in parentheses, followed by parentheses? [duplicate]
(6 answers)
Closed 9 years ago.
In the Google tutorial for implementing Google+ sign-in in Flask application, I discovered that the developer often uses an awkward way of executing JavaScript code:
Instead of doing
var a = foo(bar);
I see this:
var a = (function() {
return foo(bar);
})();
What is the reason to do it the weird way?
This is a poor example. Consider the following:
var a = (function() {
var ret = {};
ret.test = "123";
function imPrivate() { /* ... */ }
ret.public = function() {
imPrivate();
}
return ret;
})();
console.log(a)
a will contain the varible test and the function public, however you can not access imPrivate. This is the common way to handle public vs private variables;
See Why is this function wrapped in parentheses, followed by parentheses? for more info.
var a = (function() {
return foo(bar);
})();
In this case this is really unnecessary, but this is not wrong and it will not throw an error.
But IIF some times uses like module pattern:
var a = (function() {
/* some other code in own scope */
return foo(bar);
})();
In this case IIF is just a module which exports something outside.
The closure function is used to encapsulate some of the attributes / methods in the function. Much like the private / public principle from other languages.
You can find more on this topic here under Module Pattern
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
JavaScript: Class.method vs. Class.prototype.method
What's the difference between creating a prototype like this:
Date.foo = function(bar) {
alert(bar);
};
And this:
Date.prototype.foo = function(bar) {
alert(bar);
};
Why/when should I use either?
in the first example, foo is a constructor method, its like a 'static' method in java. The second is like defining a method foo on a class -- it is scoped to the instance.
you would access the first like
Date.foo()
and the second like
Date d = new Date()
d.foo()
or in another method on an instance of Date like
this.foo()