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()
Related
This question already has answers here:
Can you bind 'this' in an arrow function?
(14 answers)
Closed 2 years ago.
using the bind documentation, if i replace the object (defined as module in their example), with an es6 class instance, it does not bind.
here are the docs...
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind
and here is my code...
class Foo {}
let foo = new Foo()
let fooVar = 'foo var'
let fooFunc = () => {
return this.var
}
foo['var'] = fooVar
fooFunc.bind(foo)
foo['func'] = fooFunc
// i expected this to return 'foo var', but instead get 'undefined'
foo.func()
how can i essentially add an instance method to an existing instance, and have it bind properly?
If you read the documentation about arrow function you will see that:
Does not have its own bindings to this or super, and should not be used as methods.
Therefore you cant bind a new this if it doesnt have one
This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Closed 7 years ago.
Consider this code example:
function Person(config) {
this.name = config.name;
this.age = config.age;
}
Person.prototype.getAge = function() {
return this.age;
};
var tilo = new Person({name:"Tilo", age:23 });
console.log(tilo.getAge());
Rather than defining getName() as an appendage to the constructor's prototype property, it seems to me it could be defined within the constructor to achieve the same thing? Is there any useful difference? In other words, does doing it one way or the other have any particular implementation advantage?
The constructor will create a new instance of that function with each invocation. Putting it on the prototype will save memory.
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:
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.