This is the 26th day of my participation in Gwen Challenge
1, this point
Each time a function is called, the parser passes an implicit argument inside the function. The implicit argument is this
This refers to an object, which we call the context in which the function is executed, and this refers to a different object depending on how the function is called
- Called as a function, this refers to the window
- Called as a method, this is the object on which the method is called
- Called as a constructor, this is the object being called
var name = 'global'
function fun() {
console.log(this.name)
}
var obj = {
name: 'a'.sayName:fun
}
var obj2 = {
name: 'b'.sayName:fun
}
obj.sayName()
obj2.sayName()
Copy the code
2. Factory mode
(1) Use factory mode to create objects
function createPerson(name, age) {
var obj = new Object()
obj.name = name;
obj.age = age;
obj.sayName = function() {
console.log(this.name)
}
return obj
}
var obj = createPerson('zlm'.18)
Copy the code
Note: The constructors for all objects created using factory mode are Object, so the objects created are of type Object
(2) Constructor
For the above problem, we can create a constructor. A constructor is a normal function and is created the same way as a normal function, except that constructors usually start with a capital letter.
Note: Unlike normal functions, which are called directly, constructors need to be called using the new keyword
function Person(name, age) {
this.name = name
this.age = age
}
var per = new Person('zlm', 19)
Copy the code
(3) Constructor execution flow
When the constructor is called through new, a new object is created immediately
2. Set the new object to this in the function. You can use this to reference the new object in the constructor
3. execute the code in the function line by line
4. Return the newly created object as a return value
To sum up: objects created using the same constructor are called class objects, and a constructor is called a class. Objects created using a constructor are called instances of that class
Note: you can use instanceof to check if an object is an instanceof a class
per instanceof Person
Copy the code
All objects are descendants of Object, so any Object that passes the Instanceof check is true
(4) Constructor performance
Every time the constructor is executed, the properties inside it are created once. This results in 100 times of execution, which is very costly. If extracted and defined globally, it will pollute the namespace of the global scope
function Person(name, age, gender) { this.name = name; this.age = age; Gender = gender this.sayName = fun} function fun () {}Copy the code
3, the prototype
(1) What is a prototype
For each function we create, the parser adds a property called prototype to the function, which corresponds to an object called a prototype object
- If the function is called as a normal function, Prototype has no effect
- When a function is called as a constructor, the object it creates will have an implied property whose value refers to the constructor’s prototype object, which can be accessed ptoto
- The prototype object is a common area that all instances of the same class can access, and we can set things that all objects have in common into the prototype object
- When we access an object’s properties or methods, it looks for them in the object itself and uses them if it has them, looks for them in the prototype object if it doesn’t, and uses them if it finds them
function MyClass() {
}
var mc = new MyClass()
var mc2 = new MyClass()
console.log(MyClass.prototype)
console.log(mc2.__proto__ === MyClass.prototype)
Copy the code
Add attributes to the stereotype object
MyClass.prototype.a = 123
MyClass.prototype.sayHello = function() {}
Copy the code
Note: When we create constructors in the future, we can add the properties and methods shared by these objects to the prototype constructor, so that each object can have these properties and methods without having to add them separately for each object and without affecting the global scope
(2) The object of the prototype object
A prototype object is also an object, so it also has a prototype.
- When we use an object’s properties and methods, it looks for them in itself and uses them if they exist. If not, search for the prototype Object. If not, search for the prototype of the prototype until you find the prototype of the Object.
- The prototype of an Object has no prototype
Function MyClass() {} myclass.prototype.name = 'new MyClass() console.log(Mc.name) console.log('name') in mc)Copy the code
Note: Use in to check if the object has a property. If the object does not but the prototype does, then return true. If you only want to check if the property is present in itself, use hasOwnProperty().
console.log(mc.hasOwnProperty("hasOwnProperty"))
Copy the code
Look for the prototype of the prototype
mc.__proto__.proto__.hasOwnProperty("hasOwnProperty")
Copy the code
Note: maximum two layers, son, — > father — > grandfather, usually at this time to the Object
mc.__proto__.__proto__
Copy the code