Functions and 'Function' constructor [duplicate] - javascript

This question already has answers here:
Function and Object Javascript
(2 answers)
Closed 5 years ago.
First of all, I am not asking about the difference between creating functions with function definitions (or expressions) and using the 'Function' constructor....
I know that the three of them are used in creating functions with specific differences that I already know.
What I am trying to say is that functions - as known in javascript - are first-class objects and any function is an instance of the 'Function' constructor.
My question is what is the point in javascript that makes having a constructor function especially for functions essential?
Philosophically speaking, if 'Function' constructor - which is a function - is the constructor of functions so what is the constructor of the constructor function 'Function'?
It may be a strange question but it's really confusing!

From MDN:
The Function constructor creates a new Function object. In JavaScript every function is actually a Function object.
The important part is that all functions are actually Function objects.
Function objects are useful for parsing strings as functions without the use of eval. They are useful for dynamically creating functions whose parameters and function body may not be known at compile time.

Related

javascript prototypes and inheritance [duplicate]

This question already has answers here:
__proto__ VS. prototype in JavaScript
(34 answers)
Closed 2 years ago.
My question is Object is last on prototype chain all objects inherit properties and methods from it how it is inherit methods from Function.prototype, why Object.__proto__ === Function.prototype // true, why Object.__proto__ is not null.
The fundamental Object in JavaScript has to have a way for it to be instantiated, so it requires a prototype object to aid in lines like this:
let myObj = new Object();
And that is why Object.__proto__ is not null.
The Function object is a special type of object that facilitates object instantiation via a constructor and when used this way is known as a "constructor function". So, it makes sense for Object to inherit from a Function object so that object instances can be made.
Function is also an object. So it inherits from Object too.
But don't be confused by Object.__proto__. That's something the browser adds and isn't actually part of the language. So don't rely on it or use it.
Essentially, Function.prototype is Object.__proto__.

Different ways of naming functions in javascript [duplicate]

This question already has answers here:
What is the difference between a function expression vs declaration in JavaScript? [duplicate]
(5 answers)
var functionName = function() {} vs function functionName() {}
(41 answers)
"new" operator before declaring function
(2 answers)
Closed 7 years ago.
I'm having a hard time understanding the difference between these two functions. Are they the same? It seems like in the first case, appleOne is the name of the object, and in the second case, appleTwo is the name of the function. However, I read that functions and objects are the same in Javascript, so I'm confused...
var appleOne = new function(color) {
this.color = color;
}
function appleTwo (color) {
this.color = color;
}
Reference: Code from http://www.phpied.com/3-ways-to-define-a-javascript-class/
The difference is that the object associated with the variable appleTwo is a function object, which the object associated with the variable appleOne is not a function: it is a "regular" object with the field color.
I wouldn't say that "functions and objects are the same" in JavaScript. What is true is that there are several kinds of objects in JavaScript:
regular objects
arrays
functions
regular expressions
In the first example, you used an anonymous function as a constructor, so the object you produced and assigned to appleOne is a "regular" object. The second example uses a function declaration to define a function.
If your question was not about the difference, but rather why the first case "works" (because it is not a very common pattern), there are several S.O. questions available with the answer.

Why does calling a method with parenthesis, eg. (obj.func)(), still set `this`? [duplicate]

This question already has answers here:
How does JavaScript determine when to give a function call a "this" context? [duplicate]
(2 answers)
Closed 8 years ago.
What exactly is the parsing rule in JS that results in the following:
Let's say we have this function
getThis = function(){
return this;
}
These all work as expected using the "previous dot" rule:
getThis(); //=> Window
obj = {getThis: getThis};
obj.getThis(); //=> obj
getThisTwo = obj.getThis;
getThisTwo(); //=> Window
However, this surprises me:
(obj.getThis)() //=> obj ...WAT
My intuition would be that it would behave exactly like the 3rd example (getThisTwo). Ie, the part in parentheses is evaluated, which returns an anonymous function, which is then invoked. My expectation then is that this would be Window, not obj.
Is this a special case, or is my understanding of how this is resolved faulty?
(Edited to make the reason for my confusion clearer)
Yes. The value of the this context of a call depends on the type of the function invocation.
In your case, it's a method invocation - a function that is called by a property reference. And yes, parentheses do not evaluate a property reference.
See also Nature of JS bound functions and function invocation operator and this very good answer for details.

What is the point of prototypes in JavaScript? Why not add method directly to constructor? [duplicate]

This question already has answers here:
Declaring javascript object method in constructor function vs. in prototype [duplicate]
(3 answers)
Closed 9 years ago.
So I am going over a few lessons over JavaScript at CodeAcademy, and I ran into this section regarding prototypes.
A bit from the code:
function Dog(breed){
this.breed = breed;
};
Dog.prototype.bark = function(){
console.log("Bark!");
};
I don't understand the point of doing it that way. Why not just do it this way? (The way I first learned.)
function Dog(breed){
this.breed= breed;
this.bark = function(){
console.log("Bark!");
};
}
The second way, everything is together in one block, and not separated. What is the advantage of the first one? I know that there is, I'm just not seeing it.
One difference is, that in the prototype case the function exists only once and changing it will change all objects using this prototype. In the this. case the function is repeated in every object.
This makes a difference both in footprint and semantics.
it is for performance reasons. when creating a new instance of objects and calling a native method you are eating more memory for every creation.
On the other hand when using prototype it is more memory safe regardless the amount of Object created.

Is there a way to turn a JS object into a function? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert an "object" into a function in JavaScript?
I have a theoretical question. As far as I know, {} objects come from Object, and functions inherit from that as well. Is there something user-accessible that can make {} objects callable, like functions, or is it interpreter magic?
I'm thinking of something like:
var myobj = {}
myobj["__call"] = function() {do_things();}
myobj();
and have it work. Is it possible? If not, why not?
Thanks!
Is there a way to turn a JS object into a function?
No.
All functions are objects, but not all objects are functions. What is the meaning of "calling" an object, if it's not already a function?
Related: What is the difference between a function object and a callable object?
From my comment below (since this seemed particularly useful to the OP):
A function is callable if it has the internal [[Call]] method. See ECMA-262 §9.11 and §13.2.1, and Table 9 on p.34. But, AFAIK, there's nothing you can do to add the [[Call]] method to an object that doesn't already have one.

Categories

Resources