This question already has answers here:
Constructor invocation without parentheses [duplicate]
(1 answer)
What is the difference between new Foo and new Foo() in javascript?
(3 answers)
Invoking a function without parentheses
(9 answers)
Can we omit parentheses when creating an object using the "new" operator?
(6 answers)
Closed 5 years ago.
var foo= function(){
this.a=1;
}
var obj= new foo;
console.log(obj);
how can object be created using foo instead of foo() ?
will the constructor will be called if i don't use parentheses ?
i think foo is also function in order to call the inside code we need parentheses but here why it doesn't need ?
Related
This question already has answers here:
How does this object method definition work without the "function" keyword?
(2 answers)
ES6 Object Literal Syntax for Methods
(1 answer)
Closed 2 years ago.
I have code that has the following functions written:
testing: function() {
///
}
what is the difference of writing it as :
testing() {
///
}
Do they mean the same thing and have the same purpose?
This question already has answers here:
Javascript lost context when assigned to other variable
(4 answers)
Class methods assigned to variable (alias) lose the value of this in Javascript [duplicate]
(2 answers)
How to access the correct `this` inside a callback
(13 answers)
Why is JavaScript bind() necessary?
(4 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 3 years ago.
I've got a library that makes api calls to a number of sub-api's. If I call a function directly all works great:
let result = await apiLib.subApi1.getFoo();
But if I call by reference, I seem to lose the class context (i.e. 'this' is undefined within the class)
const endPoint = 'Foo';
let apiLibCall = apiLib.subApi1['get' + endPoint];
let result = await apiLibCall();
Is it possibly because of how 'await' works? Or am I calling by reference incorrectly?
This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
What is the 'new' keyword in JavaScript?
(17 answers)
Closed 5 years ago.
I have the following code-
var x=function(){
alert("hello");
}
var z=x;
z();// it works nicely
Now say I modified the code a bit-
var x=function(){
this.show=function(){
alert("hello");
}
}
var z=x;// It doesn't work unless I write var z=new x();
z.show();
I am wondering what difference 'this' keyword is creating between this two function and why I need to use an extra new keyword in the second case.
This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 8 years ago.
This probably is not a new question, but where is the purpose of wrapping a function or codes inside ((function () {...})());? for instance,
//Self-evoking anonymous functions
((function () {
alert("hi");
})());
What's the difference with no wrap,
alert("hi");
I still get the same result - hi
What can you pass/ put in the brackets in the end bit - })());? and why?
Using a function creates a scope. You can have params inside and do more than just alerting.
Now you can do the same without a function, but then you will keep the state on the window object and thats something that you would like to prevent in some cases.
This question already has answers here:
Can we omit parentheses when creating an object using the "new" operator?
(6 answers)
Closed 8 years ago.
Is there any difference between
var obj1 = new Constructor;
and
var obj2 = new Constructor();
given that Constructor is a constructor function?
According to the MDN docs:
[...] "new foo" is equivalent to "new foo()", i.e. if no argument list is
specified, "foo" is called without arguments.