函数也是一个对象
function isPrimary(value) {
// create cache
if (!isPrimary.roles) isPrimary.roles = {}
// cache find
if (isPrimary.roles[value]) return isPrimary.roles[value]
// insert role to cache
isPrimary.roles[value] = value
}
isPrimary('feizao')
console.log(isPrimary.roles) // { feizao: 'feizao' }
IIFE自执行函数
弥补scope上的不足
(fuction() {})()
arguments 类数组结构
方法和函数
play() // 作为函数被调用
feizao.play() //作为方法被调用
this指向调用函数的对象
构造函数返回一个对象则由该构造函数创建的实例对象为返回的对象
const obj0 = { name: 'feizao' }
function _constructor () {
this.name = 1
return obj2
}
const obj1 = new _constructor()
console.log(obj1) // obj0