Detailed explanation of JavaScript objects, in-depth understanding of js objects

definition:

Object is the basic data type of JavaScript. Objects are also a kind of compound value: these values ​​can be accessed by name, we can think of objects as a mapping from strings to values, of course, objects are not just mappings from strings to values, in addition to their own properties, they can also be derived from prototypes. Inherited attributes.

Create objects:

1. The literal method

	var empty={} //创建一个空对象

	var obj={a:1,b:2}; //两个属性的对象

	var compObj={a:'1',b:'2',cc:{
						d:'3',
						e:'4'
					}
				}; //复杂的对象

2. Create the object in the new constructor

  var a = new Array();//创建一个空数组
  var d = new Date();//创建当前时间的Date对象

3. Prototype object method

function Person(name){
 	this.name=name;
  }
  Person.prototype.eat=function(){
	 console.log(this.name+"是个好人");
  }
 var zhangsan =new Person("张三");

4.Object.create()

   var o1=Object.create({a:1,b:2});//o1会继承属性 a和b

Query and setting of attributes

The value of the attribute can be obtained by dot (.) and square brackets ([])

   var o1={
   		a:1,
   		b:2,
   		'hello zhangsan':'张三'
   }
   o1.a //1
   o1.b //2
   o1['a']//1
   o1['b']//2
   var key='hello zhangsan';
  	   o1[key]//张三
  	 o1.key//undefined
  	 o1.hello zhangsan //报错

It can be seen that for the dot (.), the right side must be a simple identifier named after the attribute name . For square brackets, inside the square brackets is an expression whose result is a string , and this string is the name of the attribute , such as:

The attribute name is'hello zhangsan', because there is a space in the middle, it can only be obtained by means of'square brackets', while other attributes can be either dotted or square brackets.

The setting value is the same, but there is an assignment operation:

 o1.a='3'
 o1['b']='4'

Delete attribute

 delete o1.a;
 delete o1['b']

Note: The delete operator can only delete own attributes, not inherited attributes.

Detection attributes

1.in operator

   'a' in o1;//true 
   'b' in o1;//true
   'toString' in o1//true o继承来的属性,打开控制台可以在o1对象的__proto__属性里面找到toString

2.hasOwnPreperty() method

   o1.hasOwnProperty('a');//true
   o1.hasOwnProperty('b');//true
   o1.hasOwnProperty('toString');//false 继承来的属性为false

3. Use !==undefined

   o1.a!==undefined//true
   o1.b!==undefined//true
   o1.toString!==undefined// 继承来的属性为true

Of course, if the value is directly set to undefined, this detection will not work. For example, var o1={a:undefined}, the a attribute cannot be detected in this way.

Enumeration of attributes

 1.for/in method

  for(key in o1){
   	console.log(key,'===',o1[key])
   }

2.Object.keys method

   var keys = Object.keys(o1);
   keys.forEach((key)=>{
   	 	console.log(key,'===',o1[key])
   })

Prototype properties

    1. The __proto__ of the object points to the prototype of its own constructor.
    2. The constructor attribute in the prototype points to the constructor itself.
    3. The isPrototypeOf method detects whether the object itself is in the prototype of another object or in the prototype chain.


Object method

The basic object inherited from the prototype object is the method, look at a few more important

1. The valueOf() method returns the original value of the specified object.

//Array:返回数组对象本身
var array = [1,3,4,6];
console.log(array.valueOf() === array);   // true

// Number:返回数字值
var num =  123213;
console.log(num.valueOf());   // 15.2654

// boolean:返回布尔值true或false
var bool = true;
console.log(bool.valueOf() === bool);   // true

2. The toString() method returns a string representing the object.

You can use this method to detect the type of object, the following code

	var toString = Object.prototype.toString;

	console.log(toString.call(new Object()));//[object Object]
	console.log(toString.call(new Date())); // [object Date]
	console.log(toString.call('abc')); // [object String]
	console.log(toString.call(123)); // [object Number]
	console.log(toString.call(true)); //  [object Boolean]
	console.log(toString.call(undefined)); // [object Undefined]
	console.log(toString.call(null)); // [object Null]
	console.log(toString.call(/./));//[object RegExp]

3. The toLocaleString() method returns a string representation of the object.

	var toString = Object.prototype.toLocaleString;

	console.log(toString.call(new Object()));//[object Object]
	console.log(toString.call(new Date())); // Wed Dec 23 2020 20:09:24 GMT+0800 (中国标准时间)
	console.log(toString.call('abc')); // abc
	console.log(toString.call(123)); // 123
	console.log(toString.call(true)); //  true
	console.log(toString.call(/./));// /./

4. The hasOwnProperty() method returns a boolean value, indicating whether the object itself has the specified property.

    var o1={
   		a:1,
   		b:2,
   		'hello zhangsan':'张三'
   }
   
   console.log(o1.hasOwnProperty('a'));//true
   console.log(o1.hasOwnProperty('b'));//true
   console.log(o1.hasOwnProperty('toString'));//false 继承来的属性为false

5. The propertyIsEnumerable() method returns a boolean value indicating whether the specified property is enumerable.

	//propertyIsEnumerable() 方法返回一个布尔值,表示指定的属性是否可枚举。
    var o1={
   		a:1,
   		b:2,
   		'hello zhangsan':'张三'
   }
   
   console.log(o1.propertyIsEnumerable('a'));//true
   console.log(o1.propertyIsEnumerable('b'));//true
   console.log(o1.propertyIsEnumerable('toString'));//false 继承来的属性为false

6. The isPrototypeOf() method is used to test whether an object exists in the prototype chain of another object.

   function Person(){}
   function Man(){}
   Man.prototype = Object.create(Person.prototype);
   
   var m = new Man();

	console.log(Person.prototype.isPrototypeOf(m)); // true
	console.log(Object.prototype.isPrototypeOf(m)); // true

From the figure below, we can also see that the __proto__ of the m instance object points to the Person.prototype object, and the __proto__ attribute of the Person.prototype object points to the Object.prototype object.

It's not easy to organize, please give me support for Sanlian, thank you!

Guess you like

Origin blog.csdn.net/dkm123456/article/details/111596659