Object of JavaScript Foundation
- 2021-12-05 05:11:00
- OfStack
1. Objects
1.1 What is an object?
In JavaScript, an object is an unordered collection of related properties and methods, and everything is an object, such as strings, numeric values, arrays, functions, etc.
Object is composed of properties and methods
Attribute: The characteristic of a thing, which is represented by an attribute in an object Method: The behavior of things is represented by methods in objects
1.2 Why do you need objects
When you save 1 value, you can use variables, and when you save (1 set of values), you can use arrays. What if you want to save the complete information of one person?
For example, the way to save Zhang 3's personal information in an array is:
var arr = [' Zhang 3',' Male ',123,156];
Save Zhang 3's personal information in the form of objects, which is clearer.
2. 3 Ways to Create Objects
2.1 Create objects with object literals {}
Object literal: It is {} that contains the properties and methods of (an object) that express this specific thing.
<script>
// Creating Objects with Object Literals {}
var obj = {};// Created the 1 An empty object
var obj = {
uname: ' Zhang 3',
age: 18,
sex: ' Male ',
sayhi: function () {
console.log('hi');
}
};
//(1) The properties or methods in it are in the form of key-value pairs Key Attribute name: Value Attribute value
//(2) Multiple properties or methods are separated by commas
//(3) The method is followed by 1 Anonymous function
//2. Using objects
//(1) Invoke the properties of an object We take Object . The way of attribute name
console.log(obj.uname);
//(2) Invoke the properties of an object Object name [' Attribute name ']
console.log(obj['age']);
//(3) Call the method of the object Object name . Method name
obj.sayhi();
</script>
2.2 Create Objects with new Object
// // Utilization new Object Create an object
var obj = new Object();// Created the 1 An empty object
obj.uname = ' Zhang 3';
obj.age = 18;
obj.sex = ' Male ';
obj.sayhi = function () {
console.log('hi~');
}
console.log(obj['uname']);
console.log(obj.sex);
obj.sayhi();
//(1) We're using Equal sign = Method of assignment Add properties and methods of an object
//(2) Use between each property and method ; End
// Case
var Object = new Object();
Object.uname = ' Naruto ';
Object.sex = ' Male ';
Object.age = 19;
Object.skill = function () {
console.log(' Shadow separation ');
}
console.log(Object.uname);
Object.skill();
2.3 Creating Objects with Constructors
// Why do we need to use constructors
// It is because the first two ways we create objects 1 This time can only be created 1 Objects
// Because we 1 Sub-creation 1 Objects, in which many properties and methods are much the same Using functions to realize code reuse This function is called Constructor
// Encapsulated in the constructor is Object
// Constructor Is to put our object inside 1 Some of the same properties and methods are abstracted and encapsulated in functions
Constructor: Is a special function, mainly used to initialize objects, that is, to assign initial values to object member variables, which is always used with new operator 1. We can abstract some common properties and methods in the object and encapsulate them in this function.
<script>
// Creating Objects with Constructors
// Syntax format of constructor
// function Constructor name () {
// this. Attribute = Value ;
// this. Method = function() {}
// }
// new Constructor name ();
function Star(uname, age, sex) {
this.name = uname;
this.age = age;
this.sex = sex;
this.sing = function (song) {
console.log(song);
}
}
var ldh = new Star(' Andy Lau ', 18, ' Male ');// Calling the function returns the 1 Objects
console.log(typeof ldh);
console.log(ldh.name);
console.log(ldh['sex']);
ldh.sing(' Ice rain ');
var zxy = new Star(' Jacky Cheung ', 36, ' Male ');
console.log(zxy.name);
console.log(zxy['sex']);
zxy.sing(' Li Xianglan ');
//1. Constructor name with uppercase initials
//2. Our constructor doesn't need return You can return the result
//3. We call the constructor You must use the new
//4. We just have to new Srart() Calling the function creates the 1 Objects
//5. Our properties and methods must be preceded by this
</script>
Summarize
This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!