Structure of console prototype [duplicate] - javascript

This question already has answers here:
__proto__ VS. prototype in JavaScript
(34 answers)
Closed 6 years ago.
Probably it's because I'm misunderstanding the prototype chain, but could someone explain me the prototype structure that makes this assertion true?
console.log.prototype === console.prototype
I expect it to be something like this
console.prototype.log = function(){...}
So log has the basic function prototype. How does that prototype resolves to his parent prototype ?
I tried some things that I didn't expected to work, but they work. For example, instead of doing:
var binded = console.log.bind(console,'something');
I can do this
var otherBind = console.log.bind(console.log,'something else')

Neither console.log nor console are class constructors, so their prototype properties are undefined. Since undefined === undefined, console.log.prototype === console.prototype is true.
Check out Reflect.getPrototypeOf(), that might be what you're looking for.
console.log.bind(console.log) means that this method will be called with console.log as this value. Calling the bound function works fine on Chrome and Node.js, but fails on Firefox (TypeError: 'log' called on an object that does not implement interface Console.). See console.log() called on object other than console behaves differently among different browsers.

Related

Why does Function's prototype key seem to point towards a function and not an object like Object's prototype key [duplicate]

This question already has answers here:
In JavaScript, why typeof Function.prototype is "function", not "object" like other prototype objects?
(4 answers)
Function.prototype is a function
(3 answers)
why typeof(Function.prototype) is function
(4 answers)
Closed 2 years ago.
If you look at Object in Chrome DevTools you'll see that it's prototype property points towards an object, and I expect this.
Object
Functions prototype property looks like it points towards a function which I find suprising.
Function
First of all, is this true? And if so, is there any reason why Function's prototype key points towards a function object instead of just a plain object? (Are there any big implications to this?)
Javascript follows Prototypal-Inheritance. And Object and Function are nothing but Constructor Functions. Any subsequent function or object created will hence inherit properties from the prototype (*).
With this pattern, it is obvious that a prototype of an Object points to an object and that of a function points to a function. It may be a bit confusing, hence it is recommended to follow this
"The Function prototype object is specified to be a function object to
ensure compatibility with the ECMAScript code that was created prior
to the ECMAScript 2015 specification."
(*) Be noted that functions are objects (you can add properties to them) in Javascript.

Accessing Undeclared Variables of a Java Scipt Object [duplicate]

This question already has an answer here:
why referencing non-existent property of an object in javascript doesn't return a reference error?
(1 answer)
Closed 5 years ago.
I was playing with variables, when i encountered this. I could not find out a reason for this. I want to understand this.
Below gives an exception for reference error which is obvious:
console.log(age);
but when the same thing is done on an object, it does not throw any kind of error. It prints out 'undefined' as if the variable is already declared:
var person = {};
console.log(person.age)
And interestingly when you check the 'person' object, there is no 'age' property.
I understand we can create property on objects directly like this:
person.age = 3;
and so can be done for global or local variables:
a = 3
But still, accessing something before assigning or declaring should throw an exception or error just like it happens in case of global or local scope variables.
Below gives an exception for reference error which is obvious
Yes, because there is no reference age.
but when the same thing is done on an object, it does not throw any
kind of error.
Why would it? You're referencing an object through valid reference person, and since this object doesn't have the property age the value undefined is returned.

Why does `this` refers to `o` here (o.method)() instead of a global object [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 6 years ago.
Suppose I have an object:
var o = {
prop: 3,
method: function() {return this.prop}
}
I was expecting this
(o.method)()
to return undefined, however it returned 3 meaning that this is set to o inside method. Why is it so? If you evaluate (o.method) separately, it evaluates to a standalone function, so I expected this to reference global object. Why, for example, the difference exists here:
(o.method)() vs (o.method || true)()
I know that o.method() will use o as context, the question is specifically about accessing the function first like this (o.method) and then calling it.
That's just how JavaScript's rules work. Unless you do some contortions, this usually means the thing before the . when you access the method prior to calling it. In this case, that's o.
The following statements are identical:
(o.method)();
o.method();
o.method.call(o);
o["method"]();
However, if you put the method on something else, it'll take on the meaning of the thing it's on:
var p = {prop: 42, method: o.method};
p.method(); // returns 42
var method = o.method;
var prop = 13;
method(); // returns 13
Note: As JavaScript grew to be much more than it was originally designed for, people realized that this probably wasn't the most intuitive way for this to work, so in ES6 if you use "Arrow Functions" (aka Lambda Functions) it won't rebind this.

what is the difference between obj.prototype and Object.getPrototypeOf(obj) [duplicate]

This question already has answers here:
Why is JavaScript prototype property undefined on new objects?
(5 answers)
Closed 6 years ago.
I create one object using Object.create method, so Object.create expects protoptype object as the first parameter and property decriptors are the second parameter.
var obj = Object.create({a:2},{b:{value:3}})
so, {a:2} is the prototype of the obj. but if I am looking for prototype like obj.prototype returns undefined but if i checks using Object.getPrototypeOf(obj) returning {a:2}
Can you tell me whats wrong here?
So, what is the first parameter in Object.create?
Thanks
You've found the one thing weirdest about the JavaScript language: the prototype property. In JavaScript, objects inherit properties from parent through the prototype chain.
But JavaScript works so that the prototype property of an object is not that object's prototype - it is the value that is used as the prototype for further objects created from it using new. So Car.prototype is the value that's going to be the prototype of creating a new instance with new Car().
What actually is the prototype of an object in the prototypal inheritance sense is what you can get with Object.getPrototypeOf(). You can learn more about this for example in Understanding the prototype property in JavaScript

Is constructor.prototype != __.proto.__ in javascript? [duplicate]

This question already has answers here:
__proto__ VS. prototype in JavaScript
(34 answers)
Closed 8 years ago.
I am not getting this into my head properly. I have seen a question which is related to it, but could not quite get anything out of it.
function Shane(){}
var sha = new Shane();
console.log(sha.__proto.__.__proto.__.__proto.__) //null
console.log(sha.constructor.prototype.constructor.prototype.
constructor.prototype)
//Shane [Can anyone explain me what5 is happening here]
Is constructor.prototype != .__proto.__?
Why do we have two ways to know the prototype chain?
__proto__ is the actual object used in the lookup chain to resolve methods.
prototype is the object used to create __proto__ with new.
Also, you have a typo, it should be .__proto__ not .__proto.__
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor
What really answers the question is that if
var fn = function() { ... };
var obj = new fn();
then
obj.__proto__ === fn.prototype
however
fn.prototype.constructor == fn
if fn has no preset prototype (i.e. the initial value). This is according to the specification:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4
So what you are observing is that there are objects such that
obj.__proto__ !== obj.constructor.prototype
Very subtle differnce from the equality far far above. :)
As for the second question: historical reasons + bad design.

Categories