notes/javascript
Javascript
Javscript 中的事件循环
What is Javascript & ECMA6?
What is pickling and unpickling?
How javascript is interpreted?
How memory is managed in Javascript?
Javascript Async/Await
Javascript This, Callback, Functional Programming.
原型 / 原型链 | Prototype / Proto
作用域 - 闭包 | Closure
异步、单线程后续补充
事件循环(Event Loop)
timers
I/O callbacks
idle, prepare
poll - < ---- inconming: connections, data, etc.
check
close callbacks
Grammar / Keywords
Core libs
Array.prototype.reduce():
const reducer = (accumulator, currentValue) => accumulator + currentValue;
This
Javascript 里的 This 指的东西很难确定。
比如 export module 里的 this:
exports = module.exports = {
isEmpty: function (obj) {
if (obj == null) return true;
if (isArrayLike(obj) && (this.isArray(obj) || this.isString(obj) || this.isArguments(obj))) return obj.length === 0;
return this.keys(obj).length === 0;
},
isThis: function () {
this.output()
},
output: function () {
console.log("this is module export")
}
}
比如 app.js 里的 this:
//app.js
App({
onLaunch: function () {
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}
}
})
},
const
The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned. For instance, in the case where the content is an object, this means the object's contents (e.g., its parameters) can be altered.
参考这篇文档 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
StackOverflow 上关于为什么要用 const 的讨论 - https://stackoverflow.com/questions/21237105/const-in-javascript-when-to-use-it-and-is-it-necessary
Let
ECMAScript 2015 引入了 let 关键词,
await、async
Async
The async function declaration defines an asynchronous function, which returns an AsyncFunction object.
参考这篇文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
Async 和 Await 是为了代码解耦。
Promise
The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.
如果你想更深入地了解和学习 Promise,包括如何对并行的异步操作进行控制,我们推荐阅读《JavaScript Promise迷你书(中文版)》 这本书。
用 async/await 以及 Promise 一起。
全局变量
在浏览器里,可以使用 window.variable_name 来调用全局变量
compile to js
- typescript
mocha
http://www.ruanyifeng.com/blog/2015/12/a-mocha-tutorial-of-examples.html
Angular
http://andyshora.com/unit-testing-best-practices-angularjs.html
Unit-testing
Automated Unit-testing
Nightwatch.js
Functional Programming
paradigm
- Imperative Programming(命令式编程)
- Objected Oriented Programming(面向对象编程)
coding style
ECMASCript 2016
"=>"
This is an arrow function. Arrow functions are a short syntax, introduced by ECMAscript 6, that can be used similarly to the way you would use function expressions. In other words, you can often use them in place of expressions like function (foo) {...}. But they have some important differences. For example, they do not bind their own values of this (see below for discussion).
Debug in Javascript
Node.js: Debugger
In Node.js, we can add a breakpoint with the debug feature of Node.js and adding a debugger line.
The process is slightly trickier than for Ruby or Python, buuut we don't have to install anything this time!
Let's walk through how to use debugger. In our Javascript, we add debugger to create a breakpoint. So say we have simple file called hello.js with the following:
var x = 'hello';
debugger;
We can go to that breakpoint by first running the file with debug added in, i.e. node debug hello.js:
$~: node debug hello.js
< debugger listening on port 5858
connecting... ok
break in hello.js:1
1 var x = 'hello';
2 debugger;
3
debug >
Now, at the debug prompt, we do two things:
Type c and hit enter Type repl and hit enter
debug> c
break in hello.js:2
1 var x = 'hello';
2 debugger;
3
4 });
debug> repl
Press Ctrl + C to leave debug repl
>
Congrats! Now you're at an interactive prompt at the breakpoint.
Module.exports 和 Exports
有时候我们会看到如下的写法:
var exports = module.exports = {};
这个时候,你只要记住,Module.exports 和 exports 指的是同样的东西就行了。
Object
知识点。
1、Javascript 的类的实现方式。
2、浅拷贝
3、深拷贝
http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html
http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html
http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.html
Node.js
Fileio
fs.readdir(path[, options], callback)
console.log(bufferOriginal.toString('utf8'));
// Output: This is a buffer example.