All in One - JavaScript

执行环境

  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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • JavaScript语言精粹 前言 约定:=> 表示参考相关文章或书籍; JS是JavaScript的缩写。 本书...
    微笑的AK47阅读 647评论 0 3
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,721评论 0 3
  • 第一章 错误处理: 错误: 程序运行过程中,导致程序无法正常执行的现象(即bug) 现象: 程序一旦出错,默认会报...
    fastwe阅读 1,243评论 0 1
  • @转自GitHub 介绍js的基本数据类型。Undefined、Null、Boolean、Number、Strin...
    YT_Zou阅读 1,305评论 0 0
  • js基础 1.javaScript的数据类型有什么 基本数据类型:Undefined、Null、Boolean、N...
    说书人_子将阅读 573评论 0 0

友情链接更多精彩内容