The main learning today is the concept of objects and the creation of objects.
22. Basic Packaging type
Yesterday when learning the built-in object in JS, the string as the basic data type also has the properties and methods of the object, this is to say that the string becomes the basic wrapper type.
The basic wrapper type is called with the following steps:
1 Sir into a temporary object of the basic wrapper type, var temp = new String ();
2 Use this temporarily created object to invoke the properties and methods of the temporary object.
3 After the call is complete, destroy immediately, temp = null;
The three steps above make the string have properties and methods of the object.
23. Concept and role of objects 23.1 concepts
An object is a collection of characteristics and behaviors that store a class of events, or a collection of key-value pairs.
A property of an object that is composed of the characteristics of an event, a method that forms an object by the behavior of the event.
23.2 effects
1 can help us encapsulate a class of data and facilitate our transmission and control of these data.
2 can help us describe a class of things.
24. Creating a custom object 24.1 using the object constructor to create an object
var student = new Object (); Use the built-in constructors to create an empty object named student.
Student.name = "Zhang San";//assigns attributes and property values to the object.
Student.sex = "male";
Student.age = 20;
Student.sayhi = function () {//assigns the method name and method value to the object.
Console.log ("I Call" +student.name+ ", I this year" +student.age+ ", I Am" +student.sex+ "the Birth");
};
Student.sayhi ();//The object method is called by the object name. method.
However, if you need to create more than one class of objects at the same time, creating an object using the above method will cause code redundancy and seriously affect the speed of the program.
So we encapsulate the above code in a function that is used to create objects of the same type in bulk.
24.2 creating objects in bulk
function Createstudent (name,age,sex) {
var student = new Object ();//each call to a function uses the built-in constructor to create an empty object named student.
Student.name =name;
Student.age = age;
student.sex= sex;
student.sayhi= function () {
Console.log ("I Call" +student.name+ ", I this year" +student.age+ ", I Am" +student.sex+ "the Birth");
};
Return student;//the creation object needs to be returned for each invocation of a function.
}
However, there is still code that can be reused within this encapsulation function.
24.3 Creating constructors
We can create a custom constructor and use this constructor to create the object.
function Student (name,sex,age) {//We define a constructor ourselves to create the object we need.
THIS.name = name; The This keyword refers to an object created using this constructor.
This.age=age;
This.sex = sex;
This.sayhi = function () {
Console.log ("I Call" +this.name+ ", I this year" +this.age+ ", I Am" +this.sex+ "the Birth");
};
}
var ls = new Student ("John Doe", "male", 25); Use this constructor to create an object and return it.
Ls.sayhi ();
Here is the use of two keywords, one for the new keyword and one for the This keyword.
The role of the 24.4 this keyword
In JS, when an object is created by a constructor, the This keyword in the constructor refers to the object name at the time of creation.
24.5 The role of the new keyword
When you use the New keyword to invoke a constructor to create an object, the new key completes the following 4 steps:
1 Use the constructor to create an empty object.
2 The keyword this points to this empty object.
3 executes the code inside the constructor, giving the current empty object this set properties and methods.
4 Returns the this current object.
25. Creating objects from Object literals
We can create an empty object by means of var o = {}, which is how the object literal is created.
var o = {
Name: "Zhang San",
AGE:20,
Address: "Shanghai City",
Sex: "Male",
Sayhi:function () {
Console.log ("Hello everyone, my name is:" + this.name+ ", my Age is:" + this.age+ ", I am" + this.sex+ ");
}
};
O.sayhi ();
. JSON
JSON is a JavaScript object representation, and a JSON object is created in a manner similar to how objects are created by object literals, except that JSON objects require double quotation marks on the attributes created.
var O1 = {//json Object
"Name": "Zhang San",
"Age": 20,
"Address": "Shanghai City",
"Sex": "Male",
"Sayhi": function () {
Console.log ("Hello everyone, my name is:" + this.name+ ", my Age is:" + this.age+ ", I am" + this.sex+ ");
}
};
The JSON object acts as a data format for transmitting data, which can be used for data transfer with the backend.
JS Basic Learning 08