JavaScript study notes 6-variables-objects

1. Variable Object

Data types in JS:

  1. String
  2. Number
  3. Boolean
  4. Null
  5. Undefined The
    above five types belong to the basic data types. As long as the values ​​we see in the future are not the above five types, they are all objects.
    The basic data types are all a single value "hello" 123 true, and there is no connection between value and value.

In JS to represent a person's information (name, gender, age)

var name="lili";
var gender="女";
var age=22;
如果使用基本数据类型
我们所创建的变量都是独立的
不能成为一个整体

The object belongs to a composite data type, and multiple attributes of different data types can be stored in the object.

1. Classification of objects

(1) Built-in objects

  • The objects defined in the ES standard can be used in any ES implementation, such as: Math String Number Boolean. . .

(2) Host object

  • The objects provided by the runtime environment of JS, at present, mainly refer to the objects provided by the browser, such as: BOM (Browser Object Model) DOM (Document Object Model) two types of objects

(3) Custom object

  • Objects created by the developers themselves

Two, create an object

1. Create an object

The function called using the new keyword is the constructor constructor. The constructor is a function specifically used to create objects.

var obj=new Object();
//new调用Object()函数去构造函数来创建对象,然后赋值给变量obj
//使用typeof检查一个对象时,会返回object

2. Add attributes to the object

The value saved in the object is called an attribute. Adding an attribute to the object.
Syntax: object. Attribute name = attribute value;

var obj=new Object();
obj.name="lili";
obj.age=22;
console.log(obj);
//{"age":22,"name":"lili"} 

3. Read the attributes in the object

Syntax: object. attribute name

console.log(obj.name);

If you read a property that is not in the object, no error will be reported but undefined will be returned.

4. Modify the properties of the object

Syntax: object. Attribute name = new value;

5. Delete the attributes in the object

Syntax: delete object. Property name;

delete obj.name;

6. Use factory methods to create objects

Use the factory method to create objects, which can be used to create objects in batches.

function fun(name,age){
    
    
				//创建一个新的对象
				var obj=new Object();
				//向对象中添加属性
			   	 obj.name=name;
				 obj.age=age;
				 obj.sayName=function(){
    
    
					console.log(this.name);	
				} 
				//将新对象返回
				return obj;
			} 
			var obj2=fun("zhuzhu",22);
			console.log(obj2);//输出fun函数中的obj对象的属性和属性值
			obj2.sayName();//输出fun函数的sayName

Objects created using the factory method, the constructors used are all Object, so the objects created are all of the Object type, which makes it impossible for us to distinguish between many different types of objects. Need to go to the constructor

Three, attribute name and attribute value

Add attributes to the object:
obj.name="李李";name是属性名,李李是属性值

1. Property name:

(1) The attribute name of the object is not mandatory to comply with the specification of the identifier, anything can be used, but we still try to follow the specification of the identifier as much as possible.
(2) If you want to use a special attribute name (not conforming to the identifier rules), you cannot use the. Method to operate, and another method is needed:
syntax: object ["attribute name"] = attribute value;

var obj=new Object();
obj["1aa"]=789;
console.log(obj["1aa"]); 

(3) Using the form of [] to manipulate attributes is more flexible. You can directly pass a variable in [], so that the value of the variable will read the attribute name to get the attribute value.

var obj=new Object();
    obj["nihao"]="你好";
var n="nihao";
    console.log(obj[n]);

2. Attribute value

(1) The attribute value in JS can be any data type or even an object.

var obj=new Object();
var obj2=new Object();
	obj2.name="lili";
	obj.test=obj2;//将obj2设置成obj的属性值
console.log(obj.test);

{
    
    "name":"lili"}

(2) Check whether an object contains the specified attribute
in operator, by which you can check whether an object contains the specified attribute, if it has, it will return true, and if it doesn't, it will return false.
Syntax: property name in object

console.log("test2" in obj);//false

Four, basic and reference data types

The basic data type is the value stored directly in the stack, the reference data type is only the reference address, the variable refers to the memory address, and the object is stored in the heap.

  1. Variables in JS are stored in the stack memory and
    the values of the basic data types are directly stored in the stack memory.
    Value and value are independent, and modifying one variable will not affect other variables.
    Insert picture description here

  2. The object is saved in the heap memory. Every time a new object is created, a new space is opened in the heap memory, and the variable saves the memory address of the object (reference to the object).
    If two variables hold the same object reference, when one modifies properties through one variable, the other will also be affected.
    Insert picture description here

    obj2=null has nothing to do with obj.

  3. When the properties of two objects are the same, the property values ​​are also the same, the two objects are still not equal, and they are two separate objects. The memory address is not the same, it is not the same.
    Insert picture description here

When comparing two basic data types, it is simply a comparison value.
When comparing two reference data types, it is the memory address of the comparison object.

Five, object literal

1. Use object literals to create an object

var obj={
    
    };

Using object literals, you can directly specify the attributes in the object when creating the object.

Syntax: {attribute name: attribute value, attribute name: attribute value...}

  1. The attribute name of the object literal can be quoted or not. It is recommended not to add it. If you want to use some special names, you must add quotes.
  2. Attribute name and attribute value are a set of name-value pair structure
    . Use between attribute name and attribute value: connection
    . Use commas between multiple name-value pairs to separate.
    If there are no other attributes after an attribute, don’t write a comma.

Guess you like

Origin blog.csdn.net/weixin_45636381/article/details/112742229