Javascript Functions and Objects Confusion [duplicate] - javascript

This question already has answers here:
Function and Object Javascript
(2 answers)
Closed 5 years ago.
In Javascript, we have two fundamental building blocks called functions and objects. But I'm a bit confused about the phrase functions are special type of objects. Anyways, in Javascript:
We create functions like this:
function foo(){}
Now the above declared function also behaves like an object as below:
foo.staticMethod = function(){}
Ok. I understand it.
Now similarly we create objects like this:
var obj = new Object() // Not using object literal here
That means, we need a function constructor Object to make even an empty object.
But Functions are also objects. How????
So my simple question is, if Object is used to create any new object, then how it can be an object itself as it accepts a property Object.prototype or I should say how a function can be an object ?

function Object(){
return {};
}

Related

Can new be applied to any function in JavaScript? [duplicate]

This question already has answers here:
How to check if a Javascript function is a constructor
(9 answers)
Closed 1 year ago.
Many sources like MDN and this tutorial define a concept called "constructor" that should be used with the new keyword to create an instance in traditional OOP's sense. But what "constructor" means is not formally stated.
It seems to me that literally any function can be used with new. (Though function without any manipulation to this in its definition is not particularly useful when newed, because it merely returns an empty object). Is this correct?
When you use the new keyword, it creates a new object. When you create an instance of a class with new, it creates an object, and any properties in the constructor method will be initialized in the object as well.

If a string is not an object, how am I able to use built-in methods? [duplicate]

This question already has answers here:
How is a Javascript string not an object?
(2 answers)
Object and String prototypes are not prototypes of defined strings?
(3 answers)
Closed 4 years ago.
When you create an object in JavaScript, you inherit properties from the Object prototype you can utilize.
But a string is a primitive type, thus there is no prototype. So how am I able to use methods such as substr() and repeat() on a string when there is no prototype the string can inherit those methods from?
For example, when I create a new array and assign it to a variable, I type the variable name into the console and the Array prototype is listed where I have access to methods I can use. But if I create a string and assign the string to a variable, then I type the variable into the console, there is no prototype attached to it.
Does that make sense?
When you access a property on a primitive string, number, or other primitive, the JavaScript engine acts as though you've converted it to the equivalent object and then looks for the property. So for instance:
var str = "hi";
var upper = str.toUpperCase();
The JavaScript engine acts as though that code were written like this (for the purposes of accessing the toUpperCase property):
var str = "hi";
var upper = new String(str).toUpperCase();
Before ES5, the spec said that the JavaScript engine actually did create a string object there and then call the property. As of ES5 that changed slightly (because in strict mode it became possible for this to have a non-object value), but in essence the concept is still the same.

What is constructor? What kind of function object can be called constructor? [duplicate]

This question already has answers here:
Constructor function vs Factory functions
(8 answers)
Closed 5 years ago.
I'm learning javascript and I'm confused with this definition.
I look up in ECMA and it defines constructor as
function object that creates and initializes obejects.
So, can any function object be called constructor?
In js its common to call a function constructor if its aim is to be called with the new operator:
var me = new Human;//Human is a constructor
However, human languages are not that strictly defined, so you can probably use it always, you just need to have good arguments for your usage. A good arguable case:
function Animal(name){//actually rather a factory function
return {name};
}
var cat = new Animal("bob");//and now ? :/
So, can any function object be called constructor?
But not every function creates or initializes objects. Consider this example:
function add(a, b) {
return a + b;
}
This function only adds two values.
What is constructor? What kind of function object can be called constructor?
I'd argue that only functions that are intended to be called with new are supposed to be considered "constructors" (that includes functions created via class).
However, you can also create objects without calling a function with new:
function getPerson(name) {
return {name: name};
}
Whether or not you consider such functions constructors is probably subjective.

Using array.prototype.push vs array.push [duplicate]

This question already has answers here:
How can I extend Array.prototype.push()?
(6 answers)
Closed 7 years ago.
I have seen people using two different ways of using different methods of the Array object in javascript.
I mostly use it like so:
arr.push(element1, ..., elementN)
But I have seen people using this:
Array.prototype.push.apply(this,arguments)
I understand that all JavaScript objects inherit the properties and methods from their prototype. The Object.prototype is on the top of the prototype chain.
What are the differences between two approaches and when should each approach be used?
The call via .apply() is used when you're interested in using .push() with an object that isn't really an array. A jQuery object, for example, is not really an array instance, but the code mostly maintains a .length property that's enough for it to look like an array, at least as far as .push() and other Array prototype methods are concerned.
For a real array instance, there's no need to do that; the .push() method is directly available via the prototype chain.
So:
var obj = { length: 0 };
Array.prototype.push.apply(obj, ["hello world"]);
console.log(obj[0]); // hello world
I assume you saw Array.prototype.push.apply(this,arguments) in an function like this
function foo() {
arguments.push = function() {
Array.prototype.push.apply(this,arguments);
}
arguments.push(1,2,3);
//....
}
Here this is the foo's arguments, it is just an Array like object, not an Array, it does not have push method. So we should use Array.prototype.push.apply(this,arguments)

JavaScript Function Declaration vs Object Literal [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Javascript object creation using literals vs custom constructor functions
Function vs. Object Literal Notaion - Which is better, or is it a matter of preference?
Could anyone enlighten me what is the difference between below two ways of creating objects in JavaScript or more specifically drawbacks/advantages of them:
Function Foo() {
function bar () {...}
}
And
var Foo = {
bar : function() {}
}
Also since in latter way, there is no this keyword, how can I make it object instance member ?

Categories