Object property query, object method, understanding of deep and shallow copy

array

definition

  • Constructor
var arr = new Array();//Using the constructor definition, when there is only one parameter, it represents the length of the array, and multiple parameters represent the array elements
  • literal
var arr1 = [];//literal

iterate over the array

  • for
for(var i=0;i<arr.length;i++){
	arr[i]
}
  • forEach, no termination, no return value
arr.forEach((item,index)=>{
	item//is the element of the array
	index//array subscript
	return//. Useless
})
  • map, with a return value, returns a new array without changing the original array. You can select some properties to return
 var arr2 = arr.map((item,index)=>{
	item//is the element of the array
	index//array subscript
	return item + 1;
})
  • filter, filter elements, the return value is an array, return an array that satisfies the condition
 var arr2 = arr.filter((item,index)=>{
	//item// is the element of the array
	//index//array subscript
	return item > 5//filter conditions
})
  • reduce(cart, accumulator)
 var arr2 = arr.reduce((total,cur)=>{
	//total//The current sum of the array
	//cur//The value of the current element of the array
	return total + cur//filter conditions
},10)//The second parameter 10 is the initial value
  • every, the array elements satisfy the condition, return true, otherwise return false
 var arr2 = arr.every((item,index)=>{
	//item// is the element of the array
	//index//array subscript
	return item > 5//filter conditions
})

object

The json key must be enclosed in double quotes. Method keys are not added. Ordinary objects must be quoted if the key has reserved words

  1. Constructor
var obj1 = new Object();
    obj1.name = 'alice';
    obj1['age'] = 20;
  1. literal
var obj2 = {}//simple literal
    obj2.name = 'zs';
var obj3 = {
    name: 'ls'//Nested literals, more intuitive to see all the properties in the object
}
//The json key must be enclosed in double quotes. Method keys are not added. Ordinary objects must be quoted if the key has reserved words

Object properties and access

What is the difference between obj.name and obj['name']?

[] value, must be a variable.
.must be a string

Delete property: delete obj.name

Detect properties

  1. in operator, both the own property and the inherited property will return true
var exist = 'name' in obj;//Does 'name' exist in obj
  1. hasOwnProperty, only its own properties can be detected. Inherited properties cannot be detected (that is, properties of prototype objects cannot be detected. Person.prototype.color = 'red';p1.hasOwnProperty('color'))//false, because color is inherited
obj.hasOwnProperty('name');

iterate over objects

for(var key in obj){
  key;//is the key
  obj[key];//is the value
}

If you use for in to traverse the array, then key is the subscript index and arr[key] is the value

function

function definition

  1. function declaration
function f() {
 }
  1. function expression
var fn = function () {
}

the difference
Pre-parse, var and function are parsed ahead of time, and the function call position is different

function has higher priority than var

function f1() {
    console.log(1111);
}
var f1;
console.log(f);//Function
var f1 = 222;
console.log(f);//222

Constructor

//Constructor
function Person(name,age) {
    this.name = name;
    this.age = age;
}
//instantiate
var tom = new Person('tom','20');

Serialization

JSON.stringify();//Convert to string
JSON.parse();//Convert string to object

deep copy and shallow copy

  1. related to primitive types and reference types
    Shallow copy: For example, if the property of the object changes, then the original value is also changed, the shadow
    Deep copy: For example, the property of the object changes, then the original value is not changed, twin
  2. Array first-level copy. Two-dimensional, array objects say otherwise
    concat() slice()
  3. Deep and shallow copy is for multi-level not one-level
  4. Object shallow copy: rewrite it again
    var a = {id:1}
    var b = {};
    b.id = a.id;
    b.id = 22;

Tags: Javascript

Posted by davissj on Sun, 15 May 2022 12:05:27 +0300