The title
This is a problem encountered in the brush, I did not do right. There are some knowledge points do not understand, so write down, learn, also incidentally share nuggets, hope to help others (cheat Bozan). Recommended reading duration: 5min
The first line of
var a = {
n: 1
}
a.x = a = {
n: 2
}
console.log(a.x)
Copy the code
The name of the second
Object.prototype.a = 1
Function.prototype.a = 2
function Foo() {}
var ins = new Foo()
console.log(ins.a)
Copy the code
The third way
var a = 'outer'
var obj = {
a: 'inner'.fn: function() {
console.log(this.a)
}
}
var wyqn = obj.fn
wyqn()
Copy the code
- undefined
- 1
- outer
1. Examine the assignment of the equal sign. 2
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
parsing
The first line of
The equal sign is performed first on the right hand side equals the left hand side
- A.x = a = {n:2} is equivalent to a.x = (a = {n:2}).
- We get a.x = {n: 2}, ending the calculation of the first equal sign on the left
- Then evaluate the expression a = {n: 2} inside the brackets.
- A refers to the new object, {n:2}, so a.x equals undefined. Is equivalent to
var a = {n:1}
var a1 = {n:2}
a.x = a1,
a = a1
console.log(a.x);
Copy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The name of the second
As an aside, we all know that functions are objects, just objects with special functions. Any Function can be viewed as the result of instantiation through the new operation of the Function() constructor.
Function Foo == Function. Prototype; Function Foo == Function. Prototype;
If Function is an instance object, what is the prototype object? This is the result of instantiation of the Object() constructor’s new operation, so its prototype Object is Object.prototype.
When you perform
var ins = new Foo();
Copy the code
What’s actually being done internally is
var ins = new Object(a); ins.__proto__ = Foo.prototype; Foo.call(ins);Copy the code
Object. GetPrototypeOf (ins). A = = Foo prototype. A. Object.getprototypeof (ins).a.call(ins)== foo.prototype.a.call (ins)
It checks whether the INS has an A attribute. If it doesn’t, it looks for Object.getProtoTypeof (ins). A. If it still doesn’t, it continues to look for Object.getProtoTypeof (Object.getProtoTypeof (ins)).
Additional material: MDN on inheritance and prototype chains
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The third way
There are three kinds of calls in JS
func(p1, p2)
obj.child.method(p1, p2)
func.call(context, p1, p2)
Copy the code
Of the above three forms, we probably use the above two, and the third one not so much. But the third is what we’re supposed to know. The other two are just grammar candy.
Func (p1, p2) is equivalent to func.call(undefined, p1, p2)
Copy the code
Obj.child-method (p1, p2) = obj.child-method. call(p1, p2)Copy the code
Wyqn () can be thought of as wyqn.call(), since no context is passed. So this is undefined.
Finally, the browser will give you a default this — window object.
conclusion
Javascript often test points are reflected in these three questions, which involve some knowledge points may not be mentioned. Welcome to point out any misunderstandings I may have. I hope we can progress and grow together.