JS basics: objects

Introduction to the object

1. Built-in objects

  • Objects defined by ES standards can be used in any ES standards
  • 比如:Math String Number Boolean Function Object

2. Host Object

  • The objects provided by the JS runtime environment are currently mainly provided by the browser. Such as BOM DOM

3. Custom objects

  • An object created by the developer himself.

The object is equivalent to a plastic bag, which contains attributes and values ​​to form a whole

Basic operations of objects

1. Create an object

  • The function called using the new keyword is the constructor
  • Constructor is a function specifically used to create objects
var obj = new object();

2. Add attributes and read attributes in the object

var obj = new object();
obj.name = "George";
obj.gender = "男";
obj.age = "23"
console.log(obj.name); //读取

3. Modification and deletion

obj.name = "tom";  //对象.属性名 = 新值
delete obj.name;  //删除

Attribute name and attribute value in variable

1. Variable name

  • Variable names can make a mess. If you use a special attribute name, you cannot use the way of. To operate, you need another way of writing: object [attribute name] = attribute value
  • Reading also needs this way. Use [] to manipulate attributes more flexibly. [] Can also pass a variable, so that the value of the variable can read that attribute.
obj["123"] = 789;
obj["nihao"] = "你好";

var n = "nihao";

console.log(obj["123"]);
console.log(obj[n]);

2. Attribute value

  • The attribute value of the JS object can be any data type
var obj = new object();
obj.test = true;
obj.test = 123;
obj.test = "垃圾";
  • You can set the object to another object's properties (molls)
var obj2 = new Object();
obj2.name = "叔本华";
//将obj2设置为obj属性
obj.test = obj2;
console.log(obj.test); //打印obj2
console.log(obj.test.name);

Basic data types and reference data types

  • Basic data type: String Number Boolean Null Undefined
var a = 132;
var b = a;
a++;

console.log("a = "+a);  //132
console.log("b = "+b);  //133

Insert picture description here
Understanding: The basic data types are stored in the stack memory, and a variable name corresponds to a value. At this time a++ only changes a. Has no effect on b

  • Reference data type Object
var obj = new Object();
obj.name = "海明威";

var obj2 = obj;

obj.name = "尼采";
console.log(obj.name);  //尼采
console.log(obj2.name);  //尼采

Insert picture description here

Understanding: The reference data type saves the heap memory address in the stack memory. When the variable in the heap memory address changes, all pointers to the variable value at this address will change.

  • Setting the value of the object to null means disconnection. Disconnecting does not affect other parameters that point to the address of the secondary memory.
    Insert picture description here

Object literal: when creating an object, you can assign the properties inside

var obj = {
    
    }; //此声明方式等于 var obj = new Object();
var obj2 = {
    
    
	name:"猪八戒",
	age:25,
	gender:"男",
	test:{
    
    name:"沙和尚"}
};

You can declare properties or internal objects.

Guess you like

Origin blog.csdn.net/sinat_33940108/article/details/110923815