對象
創(chuàng)建
第一種 new Object()
var student = new Object();
student.name = "123";
student.say = function () {
console.log(this.name);
}
student.say();
第二種 創(chuàng)建一個函數(shù)去創(chuàng)建對象
var createStu = function (name,age,address) {
var student = new Object();
student.name = name;
student.age = age;
student.address = address;
student.say = function () {
console.log(this.name + "說大家好我來自" + this.address);
}
return student;
}
var stu = createStu("張三",18,"北京");
stu.say();
var stu1 = createStu("小明",19,"香港");
stu1.say();
第三種 構(gòu)造函數(shù)
function Student(name,age,address) {
//構(gòu)造函數(shù)中this被代指成對象
this.name = name;
this.age = age;
this.address = address;
this.say = function () {
console.log(this.name + "說大家好我來自" + this.address);
}
}
var stu = new Student("張三",14,"北京");
stu.say();
var address = "address";
stu["name"] = "李四";//修改屬性可以通過[]訪問
stu[address] = "香港";
stu.say();
this和new
this
- this只出現(xiàn)在函數(shù)中
- 誰調(diào)用函數(shù),this就指誰
- new 創(chuàng)建的對象墅拭,this代指被創(chuàng)建的實(shí)例對象
new
- 開辟內(nèi)存空間活玲,存儲新創(chuàng)建的實(shí)例對象
- 把this設(shè)置為當(dāng)前實(shí)例對象
- 執(zhí)行內(nèi)部代碼,設(shè)置實(shí)例對象屬性和方法
- 返回新創(chuàng)建的實(shí)例對象
function stu() {
//這里沒有返回值
this.say = function () {
console.log(this.say);//this就代指stu實(shí)例
}
}
var aaa = new stu();
console.log(aaa);//打印stu谍婉,證明new的確把對象返回了
aaa.say();
對象的字面量
var obj = new Object();
console.log(obj);
var student = {
object:obj,
name:"小明",
age:19,address:"北京",
say:function () {
console.log(this.name + "說大家好我來自" + this.address);
}};//對象字面量創(chuàng)建舒憾,類似字典形式,key可以帶""也可以不帶穗熬,不單單可以添加基本類型镀迂,也可以添加自定義類型,或者函數(shù)
console.log(student.age) ;
student.say();
console.log(typeof student.object);
json
json和對象的區(qū)別在于json的key必須加""
var json = {
"name":"張三",
"age":12,
"address":"香港"
}
//for in遍歷json
for (var key in json){
console.log(json[key]);
}
數(shù)組
var arr = new Array();//通過創(chuàng)建對象的方式創(chuàng)建數(shù)組
var arr2 = new Array(3);//這里的三被識別成長度唤蔗,里面值默認(rèn)成undefined
var arr3 = new Array(1,2,3);
var arr4 = [1,2,3];//直接創(chuàng)建數(shù)組探遵,字面量
var arr[0] = 123;//數(shù)組賦值
var arr[2] = 234;//數(shù)組創(chuàng)建出來之后如果沒賦值都是undefined,而且可以不按照順序賦值,所以arr[1]就是undefined妓柜。
遍歷
var arr1 = [];
alert(arr1.length);//當(dāng)數(shù)組創(chuàng)建出來的時候length可以訪問箱季,輸出是0
//數(shù)組遍歷
var arr = ["1","2","3","4","5"];
for (var i = 0; i < arr.length;i++){
console.log(arr[i]);
}
//把1-100之間所有的奇數(shù)放到數(shù)組中
//數(shù)組長度本身開始是0,每添加進(jìn)去一個值棍掐,數(shù)組長度自增藏雏,所以可以用length替代
var arr = [];
for (var i = 1;i <= 100;i++){
if(i % 2){
arr[arr.length] = i;
}
}
console.log(arr);
數(shù)組中常見的api
棧操作,有返回值
console.log(arr.push(1));//再最后面添加作煌,相當(dāng)于add
console.log(arr);
console.log(arr.pop());//刪除最后一個元素
console.log(arr);
隊(duì)列操作掘殴,有返回值
console.log(arr.shift());//刪除第一個元素
console.log(arr);
console.log(arr.unshift(1));//再最前面插入元素
console.log(arr);
反轉(zhuǎn)數(shù)組
var arr = ["e","d","c","b","a"];
console.log(arr.reverse());//反轉(zhuǎn)數(shù)組,返回翻轉(zhuǎn)的數(shù)組,原數(shù)組也變了
console.log(arr);
排序:按照ASCII碼表排列的粟誓,如果自定義排序方案杯巨,傳入回調(diào)函數(shù)
var arr1 = [1,3,5,7,9,2,4,6,8,10];
console.log(arr.sort());//能排字符串
console.log(arr1.sort());//只能通過第一位進(jìn)行排列,1努酸,10服爷,2,3获诈,4...
console.log(arr1.sort(function (a,b) {
return a - b;//升序排列
}));//sort參數(shù)傳回調(diào)函數(shù)仍源,設(shè)定排序規(guī)則
拼接
var arr1 = [1,2,3];
var arr2 = [4,5];
var arr3 = arr1.concat(arr2);
console.log(arr1);
console.log(arr2);
console.log(arr3);//原數(shù)組都沒變,生成拼接數(shù)組
截取
console.log(arr3.slice(2));//一個參數(shù)從索引值第二個截取到最后
console.log(arr3.slice(-2));//截取最后兩個
console.log(arr3.slice(2,4))//從索引第二個到第四個
console.log(arr3.slice(4,2))//當(dāng)前一個索引值大于后一個的時候舔涎,截取出來一個空數(shù)組
截取能操作原數(shù)組(替換)
console.log(arr3.splice(2));//一個參數(shù)從索引值截取到最后
console.log(arr3.splice(1,2));//兩個參數(shù)笼踩,第一個索引值,第二個截取長度
console.log(arr3.splice(1,2,"122","222","22222"));//三個參數(shù)亡嫌,第一個索引嚎于,第二個截取的長度掘而,第三個以后的再原數(shù)組被截取的地方插入這些元素
console.log(arr3);//[1,"122","222","22222",4,5];
給元素查索引
var arr = [1,2,3,4,1,2,3,4];
console.log(arr.indexOf(1));//從前往后查到索引立即返回,返回0
console.log(arr.lastIndexOf(1))//從后往前查索引立即返回于购,返回4
every
var arr = [1,2,3,4,5,6,7,8,9];
var bool = arr.every(function (item,index,array) {
//還可以修改元素
if (index === 2){
array[index] = "二";
}
console.log(item);
console.log(index);
console.log(array);
return true;
});//every,返回值bool,參數(shù)是回調(diào)函數(shù)袍睡,對數(shù)組總的每一個元素執(zhí)行回調(diào)函數(shù),逐項(xiàng)遍歷肋僧,如果每一項(xiàng)返回true那個every函數(shù)返回true斑胜,只要有一項(xiàng)不是true,停止遍歷嫌吠,返回false
console.log(bool);
過濾
var arr = [1,2,3,4,5,6,7,8,9];
var newArray = arr.filter(function (item,index,array) {
return item > 3 ? true : false;
});//filter,返回的是一個數(shù)組止潘,參數(shù)是回調(diào)函數(shù),定義規(guī)則辫诅,返回的數(shù)組是回調(diào)函數(shù)返回true的項(xiàng)組成的數(shù)組
console.log(newArray);
純遍歷
var arr = [1,2,3,4,5,6,7,8,9];
var aaa = "呵呵呵";
arr.forEach(function (item,index,array) {
aaa += item;
});//forEach沒有返回值凭戴,操作數(shù)組,
console.log(aaa);
映射
var arr = [1,2,3,4,5,6,7,8,9];
var newArray = arr.map(function (item,index,array) {
return item += "map";
});//映射出新數(shù)組
console.log(newArray);
some
var arr = [1,2,3,4,5,6,7,8,9];
var bool = arr.some(function (item,index,array) {
if (index === 1){
return true;
}
return false;
});//只要一個元素返回的是ture炕矮,some返回ture
console.log(bool);//返回true
清空
//第一種
var arr = [1,2,3,4,5,6];
arr.splice(0);
console.log(arr);
//第二種length可寫
arr.length = 0;
console.log(arr);
//第三種
arr = [];
console.log(arr);
String
簡單數(shù)據(jù)類型和對象數(shù)據(jù)類型
//簡單數(shù)據(jù)類型無法綁定屬性和方法
var aaa = "abc";
aaa.asd = "123";
console.log(aaa.asd);
console.log(aaa.length);//當(dāng)調(diào)用這個方法的時候簇宽,底層做了數(shù)據(jù)類型轉(zhuǎn)換,把簡單類型變成對象類型吧享,再調(diào)用length屬性
console.log(aaa.indexOf("c"));//
//對象類型
var str = new String("abc");
str.qwe = "1234";
console.log(str.qwe);
給索引查字符
var str1 = new String("abcd");
console.log(str1.charAt(0));
console.log(str1.charAt(1));
console.log(str1.charAt(2));
console.log(str1.charAt(3));
console.log(str1.charCodeAt(0));//返回ASCII碼值
給字符查索引
var str2 = new String("abcdefg我是");
console.log(str2.indexOf("bc",0));//從前往后查找第一個符合元素的位置,第二個參數(shù)是從哪個位置開始查,返回的如果是-1譬嚣,代表沒有查到
console.log(str2.lastIndexOf("bc",0));//從后往前查钢颂,
拼接
var str3 = "1234".concat("123");
console.log(str3);
截取
var str3 = "1234567";
//slice
var str4 = str3.slice(2,3);//從索引開始截取,到索引結(jié)束拜银,一個參數(shù)就是一直截到最后
console.log(str3.slice(-3));//從倒數(shù)第幾個截到最后
console.log(str3.slice(5,2));//前大后小殊鞭,截的是空字符串
console.log(str4);
//substr
var str5 = str3.substr(2,3);//從索引開始截取,第二個參數(shù)是長度
console.log(str5);
console.log(str3.substr(-3));//從倒數(shù)第幾個截到最后
//substring(跟slice差不多尼桶,但是更智能一些)
var str6 = str3.substring(2,3);//從索引開始截取操灿,到索引結(jié)束,一個參數(shù)就是一直截到最后
console.log(str6);
console.log(str3.substr(-3));//負(fù)值的話截取的是所有的字符串
console.log(str3.slice(5,2));//前大后小泵督,智能調(diào)換
大小寫轉(zhuǎn)換
var str10 = "ABC";
console.log(str10.toLowerCase());//轉(zhuǎn)成小寫
console.log(str10.toLowerCase().toUpperCase());//轉(zhuǎn)成大寫
其他方法
var str7 = " 1 2 3 4 ";
console.log(str7.trim());//去除前后空格趾盐,中間空格去除不掉
var str8 = "1234512345";
console.log(str8.replace(/1/g,"234"));//把什么替換成什么,前面是正則
var str9 = "123456789";
console.log(str9.split(""));//把字符串變成數(shù)組,用參數(shù)切
URI
//uri小腊,數(shù)據(jù)傳遞的時候經(jīng)常需要編碼后再進(jìn)行傳遞
var url = "https://www.baidu.com?username='aaa'&password='123456'";
console.log(encodeURIComponent(url));//編譯
console.log(decodeURIComponent(encodeURIComponent(url)));//反編譯