68 Specific ways to harness the power of javascript.
- 第 1 章 让自己习惯 JavaScript
- 第 2 章 变量作用域
- 第 3 章 使用函数
- 第 4 章 对象和原型
- 第 5 章 数组和字典
- 第 6 章 库和 API 设计
- 第 7 章 并发
Wunderlist
- 定制学习目标FireFox -> 代码草稿纸
- 运行书中的JavaScript例子。GitHub -> Issues
- 分享学习笔记
MIT
68 Specific ways to harness the power of javascript.
License: MIT License
68 Specific ways to harness the power of javascript.
Wunderlist
- 定制学习目标FireFox -> 代码草稿纸
- 运行书中的JavaScript例子。GitHub -> Issues
- 分享学习笔记MIT
笔记
/*
* 这是一张 JavaScript 代码草稿纸。
*/
var s = new String("hello");
s + " world";
/*
hello world
*/
s[4];
/*
o
*/
typeof "hello";
/*
string
*/
typeof s
/*
object
*/
var s1 = new String("hello");
var s2 = new String("hello");
s1 === s2;
/*
false
*/
提示
提示
笔记
with
会让函数的行为变得不可预测with
代码块需要搜索对象的原型链来查找 with
代码块里的所有变量,因此运行速度远远低于一般的代码块。function f(x, y) {
with (Math) {
return min(round(x), sqrt(y))
}
}
f(2, 9)
/*
2
*/
不可预测会不会有人给 Math
添加 x
和 y
属性
function f(x, y) {
with (Math) {
return min(round(x), sqrt(y))
}
}
Math.x = 0
Math.y = 0
f(2, 9)
/*
0
*/
更好的办法是将局部变量显式地绑定到相关属性上
function f(x, y) {
var min = Math.min, round = Math.round, sqrt = Math.sqrt;
return min(round(x), sqrt(y));
}
Math.x = 0
Math.y = 0
f(2, 9)
/*
2
*/
提示
with
语句。with
语句隐式地绑定它们。笔记
/*
* 这是一张 JavaScript 代码草稿纸。
*/
function swap(a, i, j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
var arr = [ 100, 1000, 10000 ];
for (var i = 0; i < arr.length; i++) {
for (var j = i+1; j < arr.length; j++) {
swap(arr, i, j);
}
}
arr
/*
10000,1000,100
*/
提示
var
声明新的局部变量Unicode
代码点组成。Unicode
代码点。这个两个代码单元被称为代理对。length
、charAt
、charCodeAt
方法以及正则表达式模式(例如".")收到了影响。笔记:
NaN
是JavaScript中唯一一个不等于其自身的值,可以用过检查一个值是否等于其自身来判断该值是否是NaN。 function isRealyNaN(x) {
return x !== x
}
false
、0
、-0
、""
、NaN
、null
、undefined
,其他所有的值都为真值。undefined
更为严格的方式是使用typeof
。/*
* 这是一张 JavaScript 代码草稿纸。
*/
3 + true
/*
4
*/
// 抽象为一个工具函数
function isReallyNaN(x) {
return x !== x;
}
isReallyNaN(NaN);
/*
true
*/
isReallyNaN(2);
/*
false
*/
"J" + { toString: function() { return "S"; } };
/*
JS
*/
2 * { valueOf: function() { return 3; } };
/*
6
*/
提示:
+
是进行加法运算还是字符串连接操作取决于其参数类型。valueOf
方法强制转换为数字,通过toString
方法强制转换为字符串。valueOf
方法的对象应该实现toString
方法,返回一个valueOf
方法产生的数字的字符串表示。typeof
或者与undefined
进行比较而不是使用真值运算。笔记:
/*
* 这是一张 JavaScript 代码草稿纸。
*/
8 | 1
/*
9
*/
(8).toString(2)
/*
1000
*/
parseInt('1001',2)
/*
9
*/
0.1 + 0.2
/*
0.30000000000000004
*/
(0.1 + 0.2) + 0.3
/*
0.6000000000000001
*/
0.1 + (0.2 + 0.3)
/*
0.6
*/
(10 + 20) + 30
/*
60
*/
10 + (20 + 30)
/*
60
*/
提示:
笔记
this
关键字的初始值/*
* 这是一张 JavaScript 代码草稿纸。
*/
function averageScore(players) {
var i, n, sum;
sum = 0;
for (var i = 0, n = players.length; i < n; i++) {
sum += players[i];
}
return sum / n;
}
averageScore([7, 8, 9])
/*
8
*/
// 添加或修改全局变量会自动更新全局对象
this.foo;
/*
undefined
*/
foo = "global foo";
this.foo;
/*
global foo
*/
// 更新全局对象也会自动地更新全局命名空间
var foo = "global foo";
this.foo = "changed";
foo
/*
changed
*/
全局对象的**特殊用途:**由于全局对象提供了全局环境的动态反应机制,所以可以使用它查询一个运行环境,检测在这个平台下哪些特性是可用的。
typeof(this.JSON)
/*
object
*/
提示
(
、[
、+
、-
和/
。每一个都能作为一个表达式运算符或者一条语句的前缀,依赖于具体上下文。function square(x) {
var n = +x
return n * n
}
square(4)
/*
16
*/
function area(r) { r = +r; return Math.PI * r * r }
area(4)
/*
50.26548245743669
*/
function add1(x) { return x + 1 }
add1(4)
/*
5
*/
function area(r) { r = +r return Math.PI * r * r }
area(4)
/*
Exception: SyntaxError: missing ; before statement
@Scratchpad/2:1
*/
}
标记之前、一行的结束和程序的结束处推导分号。(
、[
、+
、-
或/
字符开头的语句前绝不能省略分号。return
、throw
、break
、continue
、++
或--
的参数之前绝不能换行。for
循环的头部或空语句的分隔符而被推导出。笔记
==
和===
运算符的行为是没有区别的。这时,最好使用严格相等运算符。==
运算符的强制转换规则,而且免除了读者不得不查找或记住这些规则的麻烦。参数类型1 | 参数类型2 | 强制转换 |
---|---|---|
null |
undefined |
不转换,总是返回true |
null 或undefined |
其他任何非null 或undenfined 的了类型 |
不转换,总是返回false |
原始类型:string、number或boolean | Date对象 | 将原始类型转换为数字;将Date对象转换为原始类型(优先尝试toString方法,再尝试valueOf方法) |
原始类型:string、number或boolean | 原始类型:string、number或boolean | 将原始类型转换为数字 |
/*
* 这是一张 JavaScript 代码草稿纸。
*/
var date = new Date("1999/12/31");
date == "1999/12/31";
/*
false
*/
date.toString();
/*
Fri Dec 31 1999 00:00:00 GMT+0800
*/
function toYMD(date) {
var y = date.getYear() + 1900,
m = date.getMonth() + 1,
d = date.getDate();
return y
+ "/" + (m < 10 ? "0" + m : m)
+ "/" + (d < 10 ? "0" + d : d);
}
toYMD(date) === "1999/12/31";
/*
true
*/
提示
==
运算符应用了一套难以理解的隐式强制转换规则。===
运算符,使读者不需要涉及任何的隐式强制转换就能明白你的比较运算。A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.