Reference type
# There are two data type values for variables in JS , values of basic types and values of reference types. The basic types are null, undefined, Boolean, string, and number. The values of reference types are references to objects, that is, a pointer to an object. The reference type is a data structure (called a class in other languages. There was no concept of class before in js, but it is in es6 (a syntactic sugar)). When it is concreted, it becomes Object, so the object is called an instance or value of a reference type. (An object is a combination of key-value pairs.) From the directory, you can see that the reference types in JavaScript are: Object type, Array type, Data type, RegExp type, Function type, basic built-in type, Single built-in type. Below I will sort out the knowledge points here. ①Reference type is a data structure used to organize data and functions together. It is also called a class. However, JavaScript does not support the basic structure of classes and interfaces, so it is called an object. definition. ②Object is the most used type. There are two ways to create Object. The first one uses the new operator:
1 var person = new Object();2 person.name = "xuchaoi";3 person.age = 24;
1 var person = {2 name: "xuchaoi",3 age: "24"4 } // 访问对象的值:person.name
1 var name = ["小明", "小红", "小青"];2 consol.log(name.join("&")); // 小明&小红&小青
1 var values = [0, 1, 5, 10, 15];2 console.log(value.sort()); //0,1,10,15,5
function compare(value, nextValue) {if(value < nextValue) {return -1; } else if(value > nextValue) {return 1; } else{return 0; } }var values = [1, 0, 10, 5, 15]; console.log(values.sort(compare)); //0,1,5,10,15
1 var colors = ["红色", "黄色", "绿色", "蓝色"];2 var colors1 = colors.slice(1); //截取从起始值到结束(数值都是从0计数)3 var colors2 = colors.slice(1,3) //截取从起始值到结束值(不包括结束值)4 console.log(colors1); //["黄色", "绿色", "蓝色"] 5 console.log(colors2); //["黄色", "绿色"]
1 //原理说明:利用indexOf只会返回数组中元素首次出现的位置与filter内函数index值的不等进行筛选2 var arry = [1,2,3,4,1,2,3]; 3 var newArray = arry.filter(function(element,index,self) {4 return self.indexOf(element) === index;5 }); //说明:filter()会遍历数组,过滤数组不符合要求的元素6 console.log(newArray); //[1,2,3,4]
1 var numbers = [1, 2, 3, 4, 5]; 2 var numbers2 = numbers.map(function(item, index, array){3 return item * 2;4 }); 5 console.log(numbers2); [2,4,6,8,10]
1 var numbers = [1, 2, 3, 4, 5]; 2 numbers.forEach(function(item, index, array){3 array[index] = item * 2;4 });5 console.log(numbers); // [2,4,6,8,10]
1 var numbers = [1, 2, 3, 4, 5]; 2 var sum = numbers.reduce(function(prev, cur, index, array) {3 return prev + cur;4 }); //reduce迭代函数接受4个参数:前一个值,当前值,项的索引,数组对象5 console.log(sum); 15
The above is the detailed content of Introduction to reference types in js programming. For more information, please follow other related articles on the PHP Chinese website!