美文网首页Cocos
All in One - JavaScript

All in One - JavaScript

作者: binyu1231 | 来源:发表于2018-02-02 17:37 被阅读7次

执行环境

  1. 浏览器: ctrl + shift + i / cmd + alt + i,
  2. native: ReactNative, Weex, Electron(chromium)
  3. Server: Nodejs
  4. cocos2d-js, weChatGame

函数是一等公民

function foo () {}
const foo = function () {} // 匿名函数

const baz = {
  foo: function () {}
}

baz.foo()

function bar () {
  const foo = () => {}
  const foo = a => a + 1
}

additional

  1. 自执行匿名函数:(function (global) {})(window) 变量注入
  2. 声明变量:默认用 const,需要的时候才用 let, 尽量别用 var 词法作用域的问题

this 在使用时才知道它的指向

const baz = {
   foo: function () { console.log(this) },
   a: 12
}
baz.foo()
// this => baz
const foo = baz.foo
foo()
window.foo()
// this => window

additional
全局变量: browser: windows, nodejs: global

异步执行,事件驱动

// browser
document.addEventListener('click', function (e) { alert(1) }, false)
fetch(url).then(() => {})
.catch(e => {})
.then(() => {}) // ....

// server
var server = http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/html'});
    response.end('<h1>Hello world!</h1>');
})
// cocos2d-js
this.node.on(cc.Node.EventType.TOUCH_END, function (e) { 
  // handle event 
})

additional

  1. 声明字符串: js 中尽量用单引号声明,因为有的时候会调用 html 中的双引号

变量

  true, 1, { a: 1 }, [1], '1'
  false, 0, null, undefined, ''
  NaN != NaN

additional

== 与 === 的区别,建议平时两个值比较时使用 === 来判断,单值判断可以直接用 if (null), undefined ? 1 : 2

类型

typeof 1 => 'number'
typeof NaN => 'number'
typeof '1' => 'string'
typeof true => 'boolean'
typeof undefined => 'undefined'
typeof [] => 'object'
typeof {} => 'object'
typeof null => 'object'

Array.isArray([]) => true
isNaN(NaN) => true

原型链 Prototype __proto__

每一个实例对象都包含一个 prototype 对象,指向它的原型,并最终指向 Object

  Object -----  Array ----- extandArray
  _proto_ --- _proto_ ----- _proto_
                               |---------------- .myExtandMethod
                 |----------<--x---------------- .map
    |---------<--x----------<--x---------------- .hasOwnPorperty

条件

if (SYNAX) {}
else if (SYNAX) {}
else {}

switch (age) {
  case 20: alert('青春')
    break
  case 40: alert('成熟')
    break
  default: 
    break
}

表达式

const a = 12
a === 6 ? '是6' : '不是6'
timer && clearTimeout(timer) // if (timer) clearTimeout(timer)
return resultArray || [] // return resultArray ? resultArray : []

example

相关文章

  • All in One - JavaScript

    执行环境 浏览器: ctrl + shift + i / cmd + alt + i, native: React...

  • FOR ALL

    All for one,one for all.

  • one for all,all for one

    最近这一段时间我都在反思自己,是不是我为别人做的事情太多了,已经超过了我应该做的范围。这样的后果就是导致,留给自己...

  • ONE OF ALL,ALL OF ONE

    很久没碰文字了,这是一个寂静的夜晚,却有着不平静心。我坐在电脑前面,对着冰冷的显示屏,看着动画片,一直没从动画片中...

  • 对话(21)

    Gl:One is all…… Gn :Less is more ! Gl:One is all…… Gn:? G...

  • ONE IS ALL

    I don’t ever want to be perfect, Cause I’m a singer that ...

  • One is all

    干嘛一定要有标签 想要做酷酷的猪猪女侠 也想有无敌的少女心 偶尔也可以文艺又温柔 百变才最可爱啊

  • One is all

    人丑就要多读书,在这大数据时代,信息的传播就像一场巨大的风暴,正在不断的改变着我们的生活、工作、习惯和思维方式,信...

  • 「All in one」

    注意:此文仅适合参与「李叫兽14天改变计划」的学员阅读 我今天的分享主题是:3 步玩转 All in one,写出...

  • ONE is all

    从2012到2016,我从高中到大学,ONE也从1.0更新到了3.0 阿,略微强迫症的我看着这些数字和标点符号好别...

网友评论

    本文标题:All in One - JavaScript

    本文链接:https://www.haomeiwen.com/subject/usivwxtx.html