一、现在还有很多浏览器不能直接使用es6语法。特别是手机端的一些低版本的浏览器。都需要用bale转换一下。
但是目前流行的框架中(vue,react,angular)。都有自己的脚手架,都能用webpack转换下。或者直接自己配置webpack , fis3,nowa 等转换。
照样不是美滋滋。
二、属性的简洁写法
//1.属性简洁表示语法
var foo = 'bar';
var obj = {foo};
console.log(obj);
//创建对象的函数
function createOjb(x = 1,y = 1){ //x = 1, y = 1; 参数的默认值
return {
x,y
}
}
var newObj = createOjb();
console.log(newObj); //{x:1,y:1}
var birthDate = '2017/8/12'
//2 方法的简写
var person = {
name:'绿巨人',
age:'200岁',
birthDate,
say(){
console.log(this.name); //等同于 say:function(){ console.log(this.name)};
}
}
person.say(); // 绿巨人
//in 方法
var msg = {
hello:'helloValue',
world:'worldValue'
}
console.log('hello' in msg,'helloValue' in msg);
// true,false; => 判断某个键值是在某个对象里面
//commonJS 模块化输出
function Obj(methods){
this.methods = methods || {};
}
Obj.prototype.getItem = function(key){
return key in this.methods ? methods[key] : null;
}
Obj.prototype.setItem = function(key,value){
this.methods[key] = value;
}
var obj = new Obj();
//module.exports = {obj};
//4.注意点 :简洁写法的属性名总是字符串,这会导致一些看上去比较奇怪的结果。
三、属性表达式
//属性名表达式
// 1. 对象添加属性的两种方式
var newObj = new Object();
newObj.name = 'html';
newObj['age'] = '20岁';
//对象字面量的方式 se5 中字面量方式下 属性名字只能用 字符串形式。不能用 ['name']
var newObj1 = {
name:'css',
age:'30岁'
}
//SE6
var newObj2 = {
['name']:'js',
['a' + 'ge']:'40岁',
['hello world']:'say hello world',
['say' + ' hi'](){
console.log(this['hello world'])
}
}
console.log(newObj2.name); // jss
console.log(newObj2['hello world']); // say hello world
newObj2['say hi'](); // say hello world
//!!!注意 属性名表达式是不能喝属性简写一起使用的
var objKey = {a:1};
var newObj3 = {
[objKey]:'我是一个对象'
}
console.log(newObj3); // {[object object]:'我是一对象'}
console.log(newObj3[{a:1}]); // 我是一个对象
console.log(newObj3['object object']); // undefined 是不是很奇怪啊
四、Object.is()
//Object.is();
//1.es5中
console.log(+0 === -0); // true
console.log(NaN === NaN); //false
//2.es6中
console.log(Object.is(+0,-0)); //false
console.log(Object.is(NaN,NaN)); //true
//在ES5中部署Object.is(); 方法
Object.defineProperty(Object,'is',{
value(x,y){
if(x === y){
return x !== 0 || 1/x === 1/y;
}
//针对NaN的情况
return x !== x && y !== y;
},
configurable:true,
enumerable:false,
writable:true
})
五、Object.assign()
//Object.assign(); 对象的合并 熟悉jquery 的人 var defaults = {a : 1} $.extend({},{},defaults || //{});
var target = {name:'es6'};
var target1 = {age:'5年'};
var target2 = {name:'es7'};
var target3 = {sex:'body'};
var defaults = {};
Object.assign(defaults,target,target1,target2,target3);
console.log(defaults); // {name: "es7", age: "5年", sex: "body"}
//从上面的defaults 的值可以看出 后面的属性会覆盖前面的属性。跟$.extend({} ,defaults || {});
// 一个道理
defaults.name = 'es8';
console.log(defaults); //{name: "es8", age: "5年", sex: "body"}
console.log(target2); //{ name:"es7"}
/*
!!! 注意 :: => 由此可以看出 Object.assign(); 是一个深拷贝的方法
!!!!!! (前提是目标某个原对象中没有对象属性)
ex:target = {
family:{
child:'七七'
}
}
目标对象属于对象属性时。就是浅拷贝啦。 跟 $.extend(true,{} , defaults || {}); 方法有区别。加了true 对象属性也进行深拷贝
*/
var tar = {
family:{
child:'七七'
}
}
var defaults1 = {
};
Object.assign(defaults1,tar);
console.log(defaults1); // { family:{child:"七七"}};
defaults1.family.child = "琪琪";
console.log(defaults1); // { family:{child:"琪琪&q