React-Native开发从入门到实战项目总结ES5与ES6常用语法总结

es5与es6常用语法教程(5)

2017-11-08  本文已影响138人  光强_上海

js常用语法系列教程如下

本节教程主要讲解以下几个常用语法

获取函数的名字

function breakfast() {}
console.log(
  breakfast.name // breakfast
)
let breakfast = function () {}
console.log(
  breakfast .name  // breakfast 
)

对于匿名函数,变量breakfast就成了函数名

let breakfast = function supreBreakfast() {}
console.log(
  breakfast .name  // supreBreakfast,匿名函数的优先级低 
)

对象表达式

let dessert = '🍰', drink = '🍺'

let food = {
  dessert: dessert,
  drink: drink ,
}

console.log(food) // {dessert: "🍰", drink: "🍺"}
let dessert = '🍰', drink = '🍺'
let food = {
  dessert,
  drink,
}
console.log(food) //{dessert: "🍰", drink: "🍺"}
let dessert = '🍰', drink = '🍺'
let food = {
  dessert,
  drink,
  breakfast() {}
}

console.log(food) // {dessert: "🍰", drink: "🍺", breakfast: ƒ}

对象的拷贝(Object.assign)

let breakfast = {}

// 将{drink: '啤酒'} 对象拷贝到breakfast对象中
Object.assign(breakfast, {drink: '啤酒'})

console.log(breakfast) // {drink: "啤酒"}

proto

let breakfast = {
  getDrink() {
    return '混泥土王'
  }
}

let dinner = {
  getDrink(){
    return '混泥土张'
  }
}

let sunday = {
  __proto__: breakfast
}

console.log(sunday.getDrink()) // 混泥土王
console.log(Object.getPrototypeOf(sunday) === breakfast) // true

// 设置对象的prototype
sunday.__proto__ = dinner
console.log(sunday.getDrink()) // 混泥土张
console.log(Object.getPrototypeOf(sunday) === dinner) // true

福利时间

上一篇下一篇

猜你喜欢

热点阅读