var f = function () {}
// ES5
// console.log(f.name);// f
// console.log(new Function().name) // anonymous
//
// demo1
function foo (){};
foo.bind({});
// console.log(foo.bind({}).name); // bound foo
// 对象简写
const foo = "bar";
const baz = {foo}; // => {foo:"bar"}
// 函数中对象简写
function foo (a, b){
console.log({a, b});
// let a = a, b = b;
}
foo(1,2);
let foo = (a, b)=>({a,b});
let age = '12'
const person = {
age,
say(){
console.log(this.age); // es6写法
}
}
person.say();// 12
/************************************************************************************/
function foo(){
console.log('foo');
}
function bar(){
console.log('bar');
}
function baz(){
console.log('baz');
}
let a = 1;
const obj = {
a,
foo,
bar,
baz
}
module.exports.obj = obj;
/************************************************************************************/
// 接收
const {a,foo,bar,baz} = reuqire('./root.js');//上面代码的地址
console.log(a,foo(),bar(),baz());
var arr = [1,2,3,4,5,6,7,8];
console.log(arr[1] === arr['1'])//true
/************************************************************************************/
let obj = {};
obj.foo = true;
obj['f'+'o'+'o'] = false;
obj.foo === obj['f'+'o'+'o'] // true
/************************************************************************************/
let a = "hello" ;
let b = "world" ;
let obj = {
[a + b]: true,
['hello'+b]: 123,
['hello'+'world']: undefined
}
// 属性名会变成字符串
/************************************************************************************/
var myObj = {};
myObj[true] = 'foo';
myObj[3] ='bar';
myObj[myObj] ='baz';// {[object Object]:"baz"}
const a = {a:1};
const b = {b:1};
const obj = {
[a]:"valueA",
[b]:"valueB"
}
console.log(obj)//{[object Object] :"valueB"};
const person = {
sayName(){
console.log('hello');
}
}
console.log(person.sayName.name);// sayName
// ES5 属性描述符
let obj = {a: 2};
console.log(Object.getOwnProperty(obj,"a"));
/**
* configurable 可配置的
* enumerable 可枚举
* writable 可写
* value 值
*/
let language = {
set current(name){
this.log.push(name);
},
log:[]
}
language.current = "English";
language.current = "Chinese";
var obj = {
_b:0,
get a(){
return this._b;
}
set a(val){
this._b = val;
}
}