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
Related
This question already has answers here:
function.name returns empty string if the function is added to an object by property
(2 answers)
Closed 1 year ago.
So, I'm using freecodecamp and it says that the following function is anonymous:
const myFunc = () => {
const myVar = "value";
return myVar;
}
console.log(myFunc.name);
Well, how come it's anonymous if its name is clearly "myFunc"?
You are setting myFunc to reference an anonymous function. myFunc has a name, the anonymous function it references does not.
const myFunc = () => {
const myVar = "value";
return myVar;
}
// This returns myFunc name
console.log(myFunc.name);
// This returns anonymous function's name which is blank
console.log(
(() => {
const myVar = "value";
return myVar;
}).name
);
This question already has answers here:
declare function properties inside
(2 answers)
Adding custom properties to a function
(10 answers)
Closed 1 year ago.
It's possible to create a method like this in a literal object :
https://jsfiddle.net/7q1530sp/2
let o = {
f: function f () {
alert("f")
}
}
o.f();
Since function is also an object, I'd like to be able to do the same within a function I tried this but it doesn't work, is there a way ?
https://jsfiddle.net/7q1530sp/1
function o(){
f: function f () {
alert("f")
}
}
o.f();
You can define as many functions-within-functions as you like!
let f1 = function() {
let f2 = function() {
let f3 = function() {
return 'functions';
};
return 'love ' + f3();
};
return 'I ' + f2();
};
console.log(f1());
If you want to define functions as properties, you could also do the following:
let f1 = function() {
return 'I ';
};
f1.f2 = function() {
return 'love ';
};
f1.f3 = function() {
return 'functions';
};
console.log(f1() + f1.f2() + f1.f3());
Note that this property assignment works exactly the same way that objects work; e.g.
let f1 = function(){}; f1.someProperty = 'someValue';
vs
let o = {}; o.someProperty = 'someValue';
The assignment of properties to a function doesn't need to occur within the function body, but functions certainly could assign properties to themselves:
let f = function() {
f.a = 'b';
f.c = 'd';
};
Note that in this case the f.a and f.c properties wouldn't exist until you actually call the function by performing: f().
You have to define it outside of the function:
function o(){
console.log("o");
}
o.f = function(){
console.log("f");
}
o.f()
You can create objects within a function in Javascript.
Here is your code:
function o() {
const d = {
f: function f() {
alert("f")
},
}
d.f();
}
o();
Output:
https://jsfiddle.net/pLjc0vbf/1/
This question already has answers here:
Assigning prototype methods *inside* the constructor function - why not?
(6 answers)
Closed 6 years ago.
For the following function I was expecting a and b to have their own versions of array, hence expecting the output to be false, but it prints true. Can anyone please explain, why ?
function Test() {
var a = [1,2,3];
Test.prototype.getArray = function() { return a; };
}
var a = new Test();
var b = new Test();
console.log(a.getArray() === b.getArray());
Every Test object inherits the same prototype. The exact same prototype. Since you're overwriting a prototype function, it's overwriting it for all objects of that type.
Example:
function log(msg) {
document.querySelector('pre').innerText += msg + '\n';
}
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function() {
log('Hello, ' + this.name);
};
var bob = new Person('Bob');
var alice = new Person('Alice');
bob.sayHello();
alice.sayHello();
// Overwriting the prototype function
Person.prototype.sayHello = function() {
log('Goodbye, ' + this.name);
};
bob.sayHello();
alice.sayHello();
<pre></pre>
If you absolutely need to access private data in a function using a standard constructor, you'll need to define the function within the constructor itself.
function Test() {
var a = [1,2,3];
this.getArray = function() {
return a;
};
}
Be aware that this negates a lot of the space and performance optimizations you gain from prototype inheritance in many implementations.
This question already has answers here:
What is the difference between a function expression vs declaration in JavaScript? [duplicate]
(5 answers)
Closed 8 years ago.
Just to carify, I'm not asking the difference of function declaration and function variable.
For example:
var Klass = function() {};
Klass.prototype._fn1 = function fn1() {};
Klass.prototype._fn2 = function fn2() {};
So, my question is what is the purpose of doing this? Why can't just write:
var Klass = function() {};
Klass.prototype._fn1 = function() {}; // <-- note that the function has no name, it just be assigned to the object as a property
Klass.prototype._fn2 = function() {};
Basically this means you are adding the method _fn1() to Klass Constructor.
Klass.prototype._fn1 = function fn1() {};
And this method is available over the object which is made by using Klass() Constructor.
myObj = new Klass();
myObj._fn1() //you can access the method like this
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();