This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the 'new' keyword in JavaScript?
creating objects from JS closure: should i use the “new” keyword?
See this code:
function friend(name) {
return { name: name };
}
var f1 = friend('aa');
var f2 = new friend('aa');
alert(f1.name); // -> 'aa'
alert(f2.name); // -> 'aa'
What's the difference between f1 and f2?
The new in your case isn't usefull.
You only need to use the new keyword when the function uses the 'this' keyword.
function f(){
this.a;
}
// new is required.
var x = new f();
function f(){
return {
a:1
}
}
// new is not required.
var y = f();
Related
This question already has answers here:
JavaScript difference between function and new function
(2 answers)
Closed 5 years ago.
Is
var myFunc = function() {
}
similar to myFunc = new function () { }
I read in the documentation it says both mean the same. What is the difference between these two?
They are not the same.
var myFunc = function(){} myFunc is a reference to anonymous function expression.
var myFunc = new function (){} reference to newly constructed instance of anonymous function expression.
var myFunc = function() {}
var myFunc1 = new function() {}
// it is the same as doing:
var myFunc2 = new myFunc;
console.log(myFunc, ' and the type is ', typeof myFunc)
console.log(myFunc1, ' and the type is ', typeof myFunc1)
console.log(myFunc2, ' and the type is ', typeof myFunc2)
You can reference this answer as well
This question already has answers here:
Nested Object Literal Access Parent
(3 answers)
Closed 6 years ago.
I have created an object within a prototype and I am trying to access a variable from the constructor with this, but the alert is returning undefined.
Constructor
function Example() {
this.param1 = 'test';
}
Prototype
Example.prototype = {
constructor: Example,
obj: {
sample:function() {
alert(this.param1); // error undifined
}
}
};
Instantiate
var o = new Example();
o.obj.sample();
Any advice would be much appreciated.
You can do this though
function Example() {
this.param1 = 'test';
}
Example.prototype = {
constructor: Example,
obj: {
sample:function(){
alert(this.param1); // error undifined
}
}
};
var o = new Example();
o.obj.sample.call(o); // <--- I use "call" to supply the context. In this case, the context would be "o"
// or
o.obj.sample.bind(o)(); // or "bind" the context, in this case "o"
This question already has answers here:
Organize prototype javascript while perserving object reference and inheritance
(3 answers)
Closed 7 years ago.
In my little brain I can't explain how to refer correctly to method in object's prototype:
function A(){}
A.prototype.action = function(){}
A.prototype.dict = { action: this.action } // Mistake here.
var a = new A();
a.dict.action(); // -> undefined, but I expect a call of 'A.prototype.action' or other function if I override it in 'a'
You haven't really explained why you need this functionality, but the following should help you avoid the errors you're seeing. Whether this is good practice or not I leave up to you to research.
function A() {
var self = this;
this.dict = {
action: function() {
// by default, A.dict.action calls A.action
return self.action();
}
};
}
// Define A.action
A.prototype.action = function() {
console.log('prototype');
};
// Let's test it out
var a = new A();
a.dict.action(); // prints 'prototype'
// Override it on instance a
a.dict.action = function() {
console.log('overridden');
};
a.dict.action(); // prints 'overridden'
// Let's create a new instance of A: b
var b = new A();
b.dict.action(); // prints 'prototpye'
This question already has answers here:
using key value pair of same object inside itself with 'this' in javascript
(3 answers)
Closed 7 years ago.
var foo = {
this.bar = function(){},
this.baz = new this.bar()
}
This won't work. I want foo.baz to be a foo.baz. How do I do it?
foo is an object literal, not an instance, so you can't use the "this" keyword in it (or semicolons, etc.)
Change your code to:
var foo = function() {
this.bar = function(){},
this.baz = new this.bar()
}
new foo();
Or:
var foo = {
bar: function() {}
}
foo.baz = new foo.bar()
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}
How are Foo and Bar any different ?
If objects were only functions, why was this new syntax introduced ? (Foo).
var Foo = function(arg) {
this.attr = arg;
};
function Bar (arg) {
this.attr = arg;
}
/*
>>> f = new Foo(3)
Object { attr=3}
>>> f.attr
3
>>> b = new Bar(40)
Bar { attr=40}
>>> b.attr
40
*/
A fair amount of documentation I'v read proposes the first syntaxe, but the second one seems to work just as well.
The difference come here:
console.log(typeof foo); //'function'
function foo() {
}
console.log(typeof bar); //'undefined'
var bar = function () {
}