一、String
-
.indexOf( 查找字符) 返回字符串中指定文本首次出现的index,若没有则-1
-
.lastIndexOf() 返回字符串中指定文本最后出现的index,若没有则-1
-
.toUpperCase() 全部转为大写
-
.toLowerCase() 全部转为小写
-
.slice(起始索引,终止索引) 截取字符串中某部分 (不包含终止索引上的值)
-
.substring (起始索引,终止索引) 截取字符串中某部分 (不包含终止索引上的值)参数不能为负
-
.substr(起始索引,截取长度) 截取字符串中某部分
-
.toLowerCase() 转化为小写
-
.toUpperCase() 转化为大写
-
.charAt(num) 返回对应索引上的值
-
.charCodeAt(num) 返回对应索引的字符编码
-
.concat(拼接内容) 拼接字符串
-
.match(‘匹配的字符’) 返回该字符对应数组 或者null
-
.search(‘查找的字符’) 返回该字符索引
-
.replace(‘被替换字符’,‘替换字符’) 用另一个值替换指定值
-
.split(“以该内容分割结果”) 转为数组
//.indexOf() 返回字符串中指定文本首次出现的index,若没有则-1
var str0="The full name of China is the People's Republic of China.";
var str1=str0.indexOf("China");
console.log(str1); //17
//.lastIndexOf() 返回字符串中指定文本最后出现的index,若没有则-1
var str0="The full name of China is the People's Republic of China.";
var str1=str0.lastIndexOf("China");
console.log(str1); //51
//.toUpperCase() 全部转为大写
var str0 = "Hello World!";
var str1 = str0.toUpperCase();
console.log(str1); //HELLO WORLD!
//.toLowerCase() 全部转为小写
var str0 = "Hello World!";
var str1 = str0.toLowerCase();
console.log(str1); //hello world!
//.slice(起始索引,终止索引) 提取字符串中某部分 (不包含终止索引上的值)
var str0 = "Apple, Banana, Mango";
var str1 = str0.slice(7,13);
console.log(str1); //Banana
var str2 = str0.slice(7);//没第二个参数就提取剩余值
console.log(str2); //Banana, Mango
var str3 = str0.slice(-5,-1)//从倒数第5项取到倒数第1项
// .substring 截取字符串 start end 从start截取到end位置结束 不包含结束位置
// substring 截取中不可以使用负数进行截取
var str = 'hello world';
console.log(str.substring(7,10)); //orl
console.log(str.substring(-5,-1)); //
//.substr(起始索引,截取长度)
var str = 'hello world';
console.log(str.substr(4,6)); //0 worl
// 将字符串转为大小写
//转小写 .toLowerCase 转大写 .toUpperCase
var str = 'hello';
console.log(str.toUpperCase()); //HELLO
console.log('HELLO'.toLowerCase()); //hello
//.charAt(num)
var str = 'hello world';
console.log(str.charAt(8));//结果为 r ,空格也占索引位置
//.charCodeAt(num)
var str = '你好 世界';
// res 获取的是 你 这个汉字的编码
var res = str.charCodeAt(1);
console.log(res);//结果为 22909 是这个汉字的编码
//.concat()
var str = 'hello world';
var res = str.concat(' 你好 世界');
console.log(res);//结果为 hello world 你好 世界
//字符串模式匹配方法 : match search replace split
// 声明正则表达式 字面量 var reg = /l/igm; var reg = new RegExp('l',igm);
var str = 'hello world';
var reg = /l/igm; //表示匹配l字母 正则表达式修饰符:i ignore 忽略大小写 g global 全局匹配 m multline 多行匹配
console.log(str.match('l')); //[ 'l', index: 2, input: 'hello world', groups: undefined ]
console.log(str.search('o')); //4
console.log(str.replace('l','L')); //heLlo world
console.log(str.replace(reg,'L')); // 将正则中字符全部替换 heLLo worLd
console.log(str.split("")); //['h', 'e', 'l', 'l','o', ' ', 'w', 'o', 'r', 'l', 'd']
console.log(str.split(" ")); //[ 'hello', 'world' ]
//.replace() 用另一个值替换指定值
var str0 = "hello, Tom!";
console.log(str0.replace("Tom", "Jerry")); //hello, Jerry!
//replace() 对大小写敏感。如需执行大小写不敏感的替换,请使用正则表达式 i(大小写不敏感):
console.log(str0.replace(/TOM/i, "Jerry")); //hello, Jerry!
//如需替换所有匹配,请使用正则表达式的 g 标志(用于全局搜索):
var str0 = "hello, Tom! hello, Jerry!";
console.log(str0.replace(/hello/g, "hi")); //hi, Tom! hi, Jerry!
//.split('以该内容为分隔') 转为数组
var str0 = "a,b,c,d,e";
console.log(str0.split(',')); //["a", "b", "c", "d", "e"]
console.log(str0.split('')); //[ 'a', ',', 'b', ',', 'c', ',', 'd', ',', 'e' ]
二、Number
内置属性(静态属性,直接调用即可)
- MAX_VALUE 可表示的最大的数
- MIN_VALUE 可表示的最小的数
- NaN 非数字值。
- NEGATIVE_INFINITY 负无穷大,溢出时返回该值。
- POSITIVE_INFINITY 正无穷大,溢出时返回该值。
- prototype 使您有能力向对象添加属性和方法。
- Math.ceil() 向上取整
- Math.floor() 向下取整
- Math.min() 取一组数字中最小数
- Math.max() 取一组数字中最大数
- Math.round() 四舍五入
- Math.random() 随机取0~1之间的数
- Math.PI 获取Π值
- Math.E 获取自然对数底数
- Math.LN10 返回 10 的自然对数(约等于2.302)
- Math.LN2 返回 2 的自然对数(约等于0.693)。
- Math.LOG10E 返回以 10 为底的 e 的对数(约等于0.434)。
- Math.SQRT1_2 返回返回 2 的平方根的倒数(约等于 0.707)。
- Math.SQRT2 返回 2 的平方根(约等于 1.414)。
内置对象(对象方法)
Math对象方法过多,只需掌握常用的即可,其他可参考Math方法链接
- .toString() 转为字符串
- .toLocaleString() 转为字符串(用本地数字格式顺序)
- .toExponential() 返回字符串值,它包含已被四舍五入并使用指数计数法的数字
- .toFixed(num) 返回字符串值,它包含了指定位数小数的数字。
- .toPrecision() 返回字符串值,它包含了指定长度的数字。
- .valueOf() 以数值返回数值。
- Number() 可用于把 JavaScript 变量转换为数值。
- parseInt() 函数可解析一个字符串,并返回一个整数。只返回首个数字
- parseFloat() 函数可解析一个字符串,并返回一个浮点数。只返回首个数字
//.toString()
var num = 123;
console.log(num.toString()); //123
//.toLocaleString()
var num = 123;
console.log(num.toLocaleString()); //123
//.toExponential() 返回字符串值,它包含已被四舍五入并使用指数计数法的数字。
var x = 9.656;
x.toExponential(2); // 返回 9.66e+0
x.toExponential(4); // 返回 9.6560e+0
x.toExponential(6); // 返回 9.656000e+0
//.toFixed() 返回字符串值,它包含了指定位数小数的数字。
var x = 9.656;
x.toFixed(0); // 返回 10
x.toFixed(2); // 返回 9.66
x.toFixed(4); // 返回 9.6560
x.toFixed(6); // 返回 9.656000
//.toPrecision() 返回字符串值,它包含了指定长度的数字。
var x = 9.656;
x.toPrecision(); // 返回 9.656
x.toPrecision(2); // 返回 9.7
x.toPrecision(4); // 返回 9.656
x.toPrecision(6); // 返回 9.65600
//.valueOf() 以数值返回数值。
var x = 123;
x.valueOf(); // 从变量 x 返回 123
(123).valueOf(); // 从文本 123 返回 123
(100 + 23).valueOf(); // 从表达式 100 + 23 返回 123
//Number() 可用于把 JavaScript 变量转换为数值。
var x1 = true;
var x2 = false;
var x3 = new Date();
var x4 = "10"
var x5 = "10 20"
Number(x1); // 返回 1
Number(x2); // 返回 0
Number(x3); // 返回 1621842783539
Number(x4); // 返回 10
Number(x5); // 返回 NaN
//parseInt() 函数可解析一个字符串,并返回一个整数。只返回首个数字:
parseInt("10"); // 返回 10
parseInt("10.33"); // 返回 10
parseInt("10 20 30"); // 返回 10
parseInt("10 years"); // 返回 10
parseInt("years 10"); // 返回 NaN
//parseFloat() 函数可解析一个字符串,并返回一个浮点数。只返回首个数字:
parseFloat("10"); // 返回 10
parseFloat("10.33"); // 返回 10.33
parseFloat("10 20 30"); // 返回 10
parseFloat("10 years"); // 返回 10
parseFloat("years 10"); // 返回 NaN
//.POSITIVE_INFINITY 表示无穷大(溢出返回)
var x = Number.POSITIVE_INFINITY;
//.NEGATIVE_INFINITY 表示负的无穷大(溢出返回)
var x = Number.NEGATIVE_INFINITY;
console.log(Math.ceil(1.1),'向上取整');//2
console.log(Math.floor(2.9),'向下取整');//2
console.log(Math.min(1,2,5,7,8,9,10),'求一组数字中的最小数字');//1
console.log(Math.max(1,2,4,5,7,9,8),'求一组数字中最大的数字');//9
console.log(Math.round(2.1),'四舍五入');//2
console.log(Math.round(2.9),'四舍五入');//3
console.log(Math.random());//0.6749361176423241
//随机点名
var arr = ['上官婉儿','萌芽','甄姬','虞姬','典韦'];
function Random(arr){
var index = parseInt(Math.random() * arr.length);
console.log(arr[index])
}
Random(arr);
console.log(Math.PI,'获取Π值'); //3.141592653589793
console.log(Math.E,'获取自然对数底数'); //2.718281828459045
console.log(Math.LN10); //2.302585092994046
console.log(Math.LN2); //0.6931471805599453
console.log(Math.LOG10E); //0.4342944819032518
Data
-
.toString() 中国标准时间
-
.toLocaleString() 年月日时分秒
-
.toLocaleDateString() 年月日
-
.getFullYear() 年
-
.getMonth() 月(返回值为0~11,所以要+1)
-
.getDate() 日
-
.getDay() 星期几(0~6,0周日,6周六)
-
.getHours() 小时
-
.getMinutes() 分钟
-
.getSeconds() 秒
-
.valueOf() 时间戳
-
.getTime() 时间戳
var now = new Date();
console.log(now.toString(),'返回的是中国标准时间');//Thu Nov 09 2023 15:32:22 GMT+0800 (中国标准时间) 返回的是中国标准时间
console.log(now.toLocaleString(),'返回的是年月日时分秒');//2023/11/9 15:32:22 返回的是年月日时分秒
console.log(now.toLocaleDateString(),'返回年月日');//2023/11/9 返回年月日
console.log(now.getFullYear(),'获取年份');//2023 获取年份
console.log(now.getMonth()+1,'获取月份');//11 获取月份
console.log(now.getDate(),'获取日期');//9 获取日期
console.log(now.getDay(),'获取星期几');//4 获取星期几
console.log(now.getHours(),'获取小时');//15 获取小时
console.log(now.getMinutes(),'获取分');//32 获取分
console.log(now.getSeconds(),'获取秒');//22 获取秒
console.log(now.valueOf(),'获取时间戳');//1699515142662 获取时间戳
console.log(now.getTime(),'获取时间戳'); //1699515142662 获取时间戳
Array
静态方法(构造函数调用)
- JSON.stringify(arr) 将数组序列化为JSON字符串(只能序列化对象可枚举的自有属性)
- JSON.parse(jsonArr) 反序列化(JSON字符串转换为数组)
- Array.isArray(变量) 判断是不是数组 ,数组返回true 非数组返回false
- Array.from(变量) 将类数组对象/字符串转为数组
- Array.of(数组元素) 创建一个数组实例(ES6 新增)
- Math.max(变量) 返回数组最大值
实列方法(自身提供)/原型方法(实例调用)
- .tostring() 转换为字符串,逗号为间隔符
- .join(“间隔符”) 转换为字符串,可调参数来自定义间隔符
- .pop() 删除最后一个数组元素
- .push(添加的元素) 添加元素到最后
- .shift() 删除首个元素
- .unshift(添加的元素) 在开头添加新元素
- .sort( [function(a,b){}] ) 字母顺序排序
- .reverse() 反转数组元素
- .splice(index ,num ,item1,item2) 改变(增删改)数组内容
以上都会修改原数组的
- .concat(被拼接数组) 连接数组
- .slice(index1,index2) 截取
- .indexOf(查找元素,何处index查) 从前往后查找想要的数组元素,返回index/-1
- .lastIndexOf(查找元素,何处index查) 从后往前查找想要的数组元素,返回index/-1
- .include(查找元素,何处index查) 从前往后查找想要的数组元素,返回值为true/false
- .forEach( 函数 ) 循环遍历数组元素
- .every( 函数 ) 判断每一个数组元素是否符合表达式 。全部符合返回true ;只要有一项不符合直接返回false 跳出循环
- .some(函数) 判断数组元素是否符合表达式。 一项符合就返回true 跳出循环 ;全部不符合才返回false
- .filter(函数) 过滤符合条件的数组元素组成新数组
- .map(函数) 返回新数组,新数组为原数组值调函数后的
// 将数组转为字符串方法
var arr = [1,2,3,4,{name:'zhangsan'}];
console.log(arr.toString(),typeof (arr.toString())); //1,2,3,4,[object Object] string
console.log(arr.join(' '),typeof (arr.join())); //1 2 3 4 [object Object] string
//JSON.stringify(arr) JSON.parse(jsonArr)
var res = JSON.stringify(arr);
console.log(res,typeof res); //[1,2,3,4,{"name":"zhangsan"}] string
console.log(JSON.parse(res)); //[ 1, 2, 3, 4, { name: 'zhangsan' } ]
//Array.isArray(变量) 判断是不是数组 ,数组返回true 非数组返回false
var obj = {name:'zhangsan'};
var a = 'hello';
var b = [1,2,3,4];
var str = 'hello';
console.log(Array.isArray(obj)); //false
console.log(Array.isArray(a)); //false
console.log(Array.isArray(b)); //true
console.log(Array.isArray(str)); //false
//Array.from() 将类数组对象/字符串转为数组
var str = 'hello';//字符串可以使用for 循环 for in循环遍历
for(var i=0;i<str.length;i++){
console.log(i,str[i]) //0 h 1 e 2 l 3 l 4 o
}
for(var index in str){
console.log(index,str[index]); //0 h 1 e 2 l 3 l 4 o
}
var res = Array.from(str);
console.log(res,Array.isArray(res)); //[ 'h', 'e', 'l', 'l', 'o' ] true
//Array.of() ES6 新增 创建一个数组实例
var arr = Array.of(10);//number整数 表示数组元素,而不是创建10个数组元素
var arr = Array.of(10,'hello',true,null,{},[],function(){});//number整数 表示数组元素
console.log(arr,arr.length); //[ 10, 'hello', true, null, {}, [], [Function (anonymous)] ] 7
var arr1 = new Array();
console.log(arr1.__proto__.constructor === arr.__proto__.constructor,arr.constructor); //true [Function: Array]
//Math.max() 返回数组最大值
var arr0=[0,1,2];
var arr1=Math.max(...arr0);
console.log(arr1); //2
以下是实例方法
//.tostring() 转换为字符串,逗号为间隔符
var arr0=["one","two","three","four"];
var arr1=arr0.toString();
console.log(arr1); //one,two,three,four
//.join() 转换为字符串,可调参数来自定义间隔符
var arr0=["one","two","three","four"];
var arr2=arr0.join("+");
console.log(arr2); //one+two+three+four
//.pop() 删除最后一个数组元素
//参数:无 返回值:被删除最后一项数组元素 修改原数组
var arr0=["one","two","three","four"];
var arr3=arr0.pop();
console.log(arr3); //four
console.log(arr0); //[ 'one', 'two', 'three' ]
//.push(添加的元素) 添加参数中的值到最后
//参数:要添加的数组元素 返回值:新数组长度 修改原数组
var arr0=["one","two","three","four"];
var arr4=arr0.push();
console.log(arr4); //4
console.log(arr0); //[ 'one', 'two', 'three', 'four' ]
var arr5=arr0.push("five");
console.log(arr5); //5
console.log(arr0); //[ 'one', 'two', 'three', 'four', 'five' ]
//.shift() 删除首个元素
//参数:无 返回值:被删除得第一个数组元素 修改原数组
var arr0=["one","two","three","four"];
var arr1=arr0.shift();
console.log(arr1); //one
console.log(arr0); //[ 'two', 'three', 'four' ]
//.unshift(添加的元素) 在开头添加新元素
//参数:要添加的数组元素 返回值:返回新数组长度 修改原数组
var arr0=["one","two","three","four"];
var arr1=arr0.unshift();
console.log(arr1); //4
console.log(arr0); //[ 'one', 'two', 'three', 'four' ]
var arr2=arr0.unshift("zero");
console.log(arr2); //5
console.log(arr0); //[ 'zero', 'one', 'two', 'three', 'four' ]
//.sort( [function(a,b){}] ) 字母顺序排序
//参数:无 返回值:根据字符编码排序后的数组 修改原数组
// 参数:function(a,b){} 返回值:排序好数组 修改原数组
var arr = [1,11,3,22,2,33];
var res = arr.sort();
console.log(res); //[ 1, 11, 2, 22, 3, 33 ]
var res = arr.sort(function(a,b){
return a-b//a-b为升序;;b-a为降序;
});
console.log(res); //[ 1, 2, 3, 11, 22, 33 ]
//.reverse() 反转数组元素
//参数:无 返回值:返回反转后的新数组 修改原数组
var arr0=["one","two","three","four"];
var arr1=arr0.reverse();
console.log(arr1); //[ 'four', 'three', 'two', 'one' ]
//.splice(index ,num ,item1,item2) 改变(增删改)数组内容
//参数:index num item、item1 返回值:被删除数组元素组成新数组 修改原数组
var arr = [1,2,3,4,5];
// 删除数组元素
var res = arr.splice(1); //从索引为1位置删除到末尾结束
var res = arr.splice(2,2); //从索引为2位置删除2项数组元素
var res = arr.splice(2,1,'hello','tom'); //从索引为2位置开始删除1项,同时增加一些项
var res = arr.splice(2,0,'hello','tom'); //从索引为2位置开始增加一些项
var res = arr.splice(-2,2); //从倒数第几项开始删除数组元素
//.concat(被拼接数组) 连接数组
//参数:被拼接的数组 返回值:返回拼接好的数组副本 不修改原数组
var arr0=["one","two","three","four"];
var arr00=["1","2","3","4"]
var arr000=["q","w","e","r"]
var arr1=arr0.concat(arr00,arr000);
console.log(arr1); //['one','two','three','four','1','2','3','4','q','w','e','r']
//.slice(index1,index2) 截取index1~index2的元素,不包括index2
//参数:startindex endindexe 返回值:返回截取后的数组元素组成新数组 不修改原数组
var arr = ['hello',true,null,undefined,{name:"zhangsan"}];
// 正数 start 从start位置截取到数组元素末尾
var res = arr.slice(2); //[ null, undefined, { name: 'zhangsan' } ]
// 正数 从下标start开始截取下标end结束
var res = arr.slice(2,4); //[ null, undefined ]
// 负数 从倒数第几项截取倒数第几inddex
var res = arr.slice(-2,-1); //[ undefined ]
var res = arr.slice(-4,3); //[ true, null ]
var res = arr.slice(2,-1); //[ null, undefined ]
//.indexOf(查找元素,何处index查) 从前往后查找想要的数组元素,然后返回index
// 参数:查找元素 从何处开始查可选(index) 返回值:返回该元素的索引或者-1(找不到) 不修改原数组
var arr = [1,2,3,4,5,3,2,1];
var res = arr.indexOf(3,3);
console.log(res); // 5
//.lastIndexOf(查找元素,何处index查) 从后往前查找想要的数组元素,然后返回index
//参数:查找数组元素 index:从何处开始从后往前查 返回值:返回元素索引或者-1(找不到) 不修改原数组
var arr = [1,2,3,4,5,3,2,1,4];
var res = arr.lastIndexOf(2,4);
console.log(res); // 1
//.include(查找元素,何处index查)
//参数:查找数组元素 index 返回值:true/false
const array = [1, 2, 3, 4, 5];
console.log(array.includes(3)); // true
console.log(array.includes(6)); // false
console.log(array.includes(2, 2)); // false,从索引2开始搜索,不包含2
console.log(array.includes(2, -2)); // false,从倒数第二个元素开始搜索,不包含2
//.forEach( 函数 ) for循环升级版 循环遍历数组元素
//参数:function(item,index,arr){} 返回值:就算设置了也没有返回值 不修改原数组
//item--->数组每一项;; index--->数组每一项所对应的下标;; arr--->数组本身
var arr = ['tom','jack','larry','terry'];
var res = arr.forEach(function(item,index,arr){
console.log(item,index,arr); //tom 0 [ 'tom', 'jack', 'larry', 'terry' ] jack 1 [ 'tom', 'jack', 'larry', 'terry' ] larry 2 [ 'tom', 'jack', 'larry', 'terry' ] terry 3 [ 'tom', 'jack', 'larry', 'terry' ]
return 1
});
console.log(res); //undefined
//.every( 函数 ) 判断每一个数组元素是否符合表达式 。全部符合返回true ;只要有一项不符合直接返回false 跳出循环
//参数:function(item,index,arr){} 返回值:true或者false 不修改原数组
var arr = [1,2,3,4,5];
var res = arr.every(function(item,index,arr){
console.log('every')
return item>0
});
console.log(res);//every every every every every true
//.some(函数) 判断数组元素是否符合表达式。 一项符合就返回true 跳出循环 ;全部不符合才返回false
//参数:function(item,index,arr){} 返回值:true或者false 不修改原数组
var arr = [1,2,3,4,5];
var res = arr.some(function(item,index,arr){
console.log('some')
return item>5
});
console.log(res)//some some some some some false
//.filter(函数) 过滤符合条件的数组元素组成新数组
// 参数:function(item,index,arr){} 返回值:返回符合条件数组元素组成新数组 不修改原数组
var arr = [1,2,3,4,5];
var res = arr.filter(function(item,index,arr){
return item>2
});
console.log(res); //[ 3, 4, 5 ]
//.map(函数) 返回新数组,新数组为原数组值调函数后的。映射 对每一个数组元素都进行操作
//参数:function(){ } 返回值:新数组,新数组值为原数组值调函数后 不修改原数组
var arr = [1,2,3,4,5];
var data = [ {id:1,name:'zhangsan'}, {id:2,name:'lisi'}, {id:3, name:'wangwu'}];
var res1 = data.map(function(item){
return item.name
});
console.log(res1); //[ 'zhangsan', 'lisi', 'wangwu' ]
var res = arr.map(function(item,index,arr){
return item+5;
});
console.log(res); //[ 6, 7, 8, 9, 10 ]
var arr0 = [3, 9];
var arr1 = arr0.map((x,i)=>({key: 'key'+i, value: x}));
console.log(arr1); //返回 [{key:"key0",value:3},{key:"key1",value:9}]
Object
静态方法(构造函数本身调用)
-
JSON.stringify(obj) 将对象序列化为JSON字符串(只能序列化对象可枚举的自有属性)
-
JSON.parse(jsonStr) 反序列化
-
Object.defineProperty(目标对象,属性,{属性特性}) 定义一个属性默认特性
-
Object.defineProperties(目标对象,{属性:属性特性},… ,{属性:属性特性}) 定义多个属性默认特性
-
Object.getOwnPropertyDescriptor(目标对象,属性名ies)) 读取对象一个自身属性的所有描述符
-
Object.getOwnPropertyDescriptos(目标对象) 读取对象所有自身属性的所有描述符
-
Object.prototype 指出该构造函数的原型对象
-
Object.prototype.constructor 指向原型对象的构造者
实列方法(自身提供)/原型方法(实例调用)
- .length
- .toString() 转换为字符串
- .valueOf() 转换为字符串,数值,布尔值
- toLocaleString() 转换为字符串,其与执行环境的地区对应
- ._proto _ 指针指向
- .constructor 保存用户创建当前对象的函数,与原型对象对应的构造函数
- .isPrototypeOf(object) 检查传入的对象原型
- .hasOwnProperty() 检查是否为自有属性:true;继承属性:false
- .propertyIsEnumerable() 检查是否为自身属性&&枚举属性:true
// JSON.stringify(obj) 将对象序列化为JSON字符串(只能序列化对象可枚举的自有属性)
//JSON.parse(jsonStr) 反序列化
var obj = {
name:'zhangsan',
age:12
};
var jsonStr = JSON.stringify(obj);
console.log(jsonStr,typeof jsonStr); //{"name":"zhangsan","age":12} string
var objParse = JSON.parse(jsonStr);
console.log(objParse); //{ name: 'zhangsan', age: 12 }
console.log(obj == objParse); //false,两者虽然内容一样,但引入地址不同
//Object.defineProperty(目标对象,属性,{属性特性}) 修改数据属性
var obj = {name:'zhangsan',age:12,gender:'male'};
Object.defineProperty(obj,'name',{
configurable:false,//默认对象属性是可以删除的 默认值true 改为false后该属性无法删除
writable:true, //表示当前属性是否可写 默认值true
enumerable:true, //表示当前属性是否可枚举 默认值true 可枚举属性可以使用for in循环遍历
value:'terry', //给name属性设置属性值
});
delete obj.name;
for(var key in obj){ console.log(key)} //name age gender
console.log(obj); //{name:'zhangsan',age:12,gender:'male'}
//Object.getOwnPropertyDescriptor(目标对象,属性名)) 读取指定对象所有自身属性的描述符
var obj = {name:'zhangsan',age:12, gender:'male'}
Object.defineProperties(obj,{
name:{writable:false},
age:{configurable:false},
gender:{enumerable:false}
});
console.log(Object.getOwnPropertyDescriptor(obj,'name')); //{ value: 'zhangsan', writable: false, enumerable: true, configurable: true }
console.log(Object.getOwnPropertyDescriptor(obj,'gender')); //{ value: male, writable: true, enumerable: false, configurable: true }
console.log(Object.getOwnPropertyDescriptor(obj,'age')); //{ value: 12, writable: true, enumerable: true, configurable: false }
//Object.prototype 指出该构造函数的原型对象
// Object.prototype.constructor 指向原型对象的构造者
var obj = new Object(); //构造函数是Object,实例是{}里的
var obj1 = {};//也是构造函数创建
// 构造函数
console.log(Object); //[Function: Object]
// 该构造函数所对应的原型对象
console.log(Object.prototype); //[Object: null prototype] {}
// 每一个原型对象都有指针指向构造者
console.log(Object.prototype.constructor === Object); //true
//._proto_ 指针指向
var obj = new Object();
var obj1 = {};
// 每一个实例对象都有一个指针指向原型对象
console.log(obj.__proto__ === Object.prototype); //true
//.hasOwnProperty() 检查是否为自有属性:true;继承属性:false
//.propertyIsEnumerable() 检查是否为自身属性&&枚举属性:true
var obj = {name:"zhangsan",age:12}
// hasOwnProperty 检测属性是否是对象自有属性 对于继承属性返回false
console.log(obj.hasOwnProperty('name')); //true
console.log(obj.hasOwnProperty('toString')); //false,toString为继承属性
console.log(obj.hasOwnProperty('valueOf')); //false
// propertyIsEnumerable 检测是否是自有属性 可枚举属性 返回true
console.log(obj.propertyIsEnumerable('name')); //true
console.log(obj.propertyIsEnumerable('age')); //true
console.log(obj.propertyIsEnumerable('toString')); //false,不可枚举
console.log(obj.propertyIsEnumerable('valueOf')); //false
console.log(obj.propertyIsEnumerable('gender')); //false