IIssNan's Notes

Quick notes


  • 首页

  • 归档

  • 标签

  • 搜索

Equality comparison in ECMAScript 262 3rd edition

发表于 2012-07-28 | | 阅读次数

问题:

1
2
![] == []; // true
"0" == 0 // true

运算法则:

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

  1. If Type(x) is different from Type(y), go to step 14.
  2. If Type(x) is Undefined, return true.
  3. If Type(x) is Null, return true.
  4. If Type(x) is not Number, go to step 11.
  5. If x is NaN, return false.
  6. If y is NaN, return false.
  7. If x is the same number value as y, return true.
  8. If x is +0 and y is −0, return true.
  9. If x is −0 and y is +0, return true.
  10. Return false.
  11. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.
  12. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
  13. Return true if x and y refer to the same object or if they refer to objects joined to each other (see 13.1.2). Otherwise, return false.
  14. If x is null and y is undefined, return true.
  15. If x is undefined and y is null, return true.
  16. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
  17. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
  18. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
  19. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
  20. If Type(x) is either String or Number and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
  21. If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y.
  22. Return false.

Prototype based programming

发表于 2012-07-18 | | 阅读次数

避免重写原型时constructor属性的指向错误

发表于 2012-07-13 | | 阅读次数

在整个替换构造函数的prototype值时,会发生对象的constructor属性执行了Object,而非构造函数。
通常的修正方法有以下两种:

1
2
3
4
5
6
7
8
9
10
11
var Foo = function(){};
Foo.prototype = {
name : "foo",
bar : function(){
console.log(this.name);
}
};
// 修正替换Foo.prototype后导致construtor指向错误
Foo.prototype.constructor = Foo;

或者使用一个闭包的方式,局部更新prototype:

1
2
3
4
5
6
(function(p){
p.name = "foo";
p.bar = function(){
console.log(p.name);
}
})(Foo.prototype);

Get a github repository as a zip file via cURL

发表于 2012-06-17 | | 阅读次数
1
curl -Lk https://github.com/someone/likeyou/zipball/master > likeyou-latest.zip

cURL manual

从参数看 JavaScript 字符串的 slice、substring 以及 substr 函数

发表于 2012-06-16 | | 阅读次数

JavaScript 字符串有三个方法用于截取部分内容,分别是:slice, substring, substr。这里通过参数的角度来区分这三个函数的不同与相同。

函数 slice

slice 函数接收的两个参数有如下的特点:

  • 一个半闭半开区间的两个端点,并且参数可为负数;
  • 第二个参数可选,默认为字符串的长度;

函数返回一个新的 这个区间之间的子字符串

1
2
3
4
5
6
7
8
9
10
var s = 'string';
// 方法调用
s.slice(start, end);
// 参数是半开半闭区间的两个端点
[
start >= 0 ? start : s.length + start,
isNaN(end) ? s.length : (end >= 0 ? end : s.length + end)
)

函数 slice 的模拟实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 随机生成8位字符串
function generateString(){
return parseInt( Math.random() * 1E8).toString();
}
// 随机生成一位数字
function generatePoint(){
return parseInt( Math.random() * 10 );
}
// slice实现
function sliceDescription(start, end){
if (isNaN(start)) {
return;
}
var s = generateString();
// end默认为字符串的长度
end = end == undefined ? s.length : end;
var result1 = s.slice(start, end);
// slice方法的实现
var result2 = '';
start = start >= 0 ? start : s.length + start;
end = isNaN(end) ? s.length : (end >= 0 ? end : s.length + end);
var length = (end > s.length ? s.length : end) - start;
for (var i = 0; i < length; i++) {
result2 += s[start + i];
}
console.log(result1 == result2);
}
// 循环10000次,随机取start和end
for (var i = 0; i < 1E4; i++) {
var start = generatePoint();
var end = generatePoint();
sliceDescription(start, end);
}

函数 substring

substring 函数接收的两个参数有如下的特点:

  • 接收两个参数,两个参数同样是一个半闭半开区间的端点值,与 slice 不同的是参数必须大于 0;
  • 函数是取 from, to 之间较小的值作为截取起点
  • 第二个参数可选,默认为字符串的长度;

函数返回一个新的 from 到 to 之间的子字符串

1
2
3
4
5
6
7
s.substring(from, to);
// 参数的表示
[
Math.min(from, to),
Math.max(from, to)
)

函数 substring 的模拟实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// 随机生成8位字符串
function generateString(){
return parseInt( Math.random() * 1E8).toString();
}
// 随机生成一位数字
function generatePoint(){
return parseInt( Math.random() * 10 );
}
function substringDescription(from, to){
if ( isNaN(from) ) {
return;
}
var s = generateString();
var result1 = s.substring(from, to);
if (from < 0 || to < 0) {
return;
}
// substring的实现
var fromNew = Math.min(from, to);
var toNew = Math.max(from, to);
var result2 = '';
var length = (toNew >= s.length ? s.length : toNew ) - fromNew;
for (var i = 0; i < length; i++) {
result2 += s[fromNew + i];
}
console.log( result1 == result2 );
}
// 调用10000次,比对结果
for (var i = 0; i < 1E4; i++) {
var from = generatePoint();
var to = generatePoint();
substringDescription(from, to);
}

函数 substr

函数 substr 接收两个参数:起点和截取长度。实际上, substr 是另外两个函数的一个变形。

1
2
3
4
5
6
// 调用方式:
s.substr(start, length);
// 等价于
s.slice(start, start + length)
s.substring(start, start + length)

最后,需要注意的是,substr 并为被纳入 ECMAScript 标准。

1…272829…37
IIssNan

IIssNan

胡编一通,乱写一气

183 日志
6 分类
111 标签
RSS
GitHub Twitter 豆瓣 知乎
© 2011 - 2017 IIssNan
由 Hexo 强力驱动
主题 - NexT.Pisces
0%