The naming convention of the object's basic knowledge object's special attribute name

Object attribute name specification:
The attribute name of the object does not obey the specification of identifiers, and keywords can be used.
However, try to comply with the specification of identifiers.
If you want to use special attribute names, you cannot use ".". The naming method requires another syntax: object [ "Property name"] =
"[]" is recommended for attribute value because "[]" is more flexible. A variable can be passed in "[]" and a variable can be passed in "[]", so that the value of the variable will be as large as possible Read the attribute of that attribute name

<script type="text/javascript">
			var obj=new Object();
			obj["123"]=789;
			console.log(obj["123"]);
			
			var a="123";
			console.log(obj[a]);
			//返回值还是obj["123"]的值
		</script>

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


<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		
		<script type="text/javascript">
			var obj=new Object();
			//调用Object()函数创建对象
			/*在对象中保存的值称为属性
			 * 向对象中添加属性
			 * 语法:对象.属性名=属性值;
			 */
			obj.name="倪蝶蝶";
			console.log(typeof obj);
			//浏览器的控制台会显示其中的属性 编译器的控制台显示Object
			obj.age=999;
			/*obj对象中含有了两个不同且无联系基本类型
			因为obj对象同时包含他们 使他们有了联系*/
			
			console.log(obj.hello);
			//如果读取对象中没有的属性则会返回undefined,不会报错 
			
			//删除对象的属性 语法:delete 对象.属性名
			delete obj.name;
			//删除onj.name(孙悟空)
		</script>
	</head>
	<body>
	</body>
</html>

The attribute value of a JS object can be any data type, even an object

<script type="text/javascript">
			var obj=new Object();
			var obj1=new Object();
			
			obj1["4"]=555;
			obj["999"]=obj1;
			console.log(obj["999"]);
			console.log(obj["999"]["4"]);
		</script>

Check whether the object contains the specified property
in operator This operator can check whether an object contains the specified property. If yes, return true. If not, return false.
Syntax "property name" in object

<script type="text/javascript">
			var obj=new Object();
			obj["123"]=789;
			
			console.log("text" in obj);
			//返回false 
			console.log("123" in obj);
			//返回true
		</script>

Guess you like

Origin blog.csdn.net/qq_45821251/article/details/108566087