This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pattern = /Java/g; | |
var text = "JavaScript is more fun than Java!"; | |
var result; | |
while((result = pattern.exec(text)) != null ){ | |
console.log("Matched '" + result[0] + "'" + | |
" at position " + result.index + | |
"; next serach begins at " + pattern.lastIndex) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isArray = Function.isArray || function(o){ | |
return typeof o === "object" && | |
Object.prototype.toString.call(o) === "[object Array]" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = [33, 4, 1111, 222] | |
a.sort() // 字母表顺序:1111,222,33,4 | |
a.sort(function(a,b){ // 数值顺序:4,33,222,1111 | |
return a - b // 根据顺序,返回复数、0、正数 | |
}) | |
a.sort(function(a,b){return b-a}) // 数值大小相反的顺序 | |
a = ['ant', 'Bug', 'cat', 'Dog'] | |
a.sort() // 区分大小写的排序:['Bug', 'Dog', 'ant', 'cat'] | |
a.sort(function(s, t){ // 不区分大小写的顺序 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (var i = 0; i < a.length; i++) { | |
if(!a[i]) continue; // 跳过null、undefined 和不存在的元素 | |
// 循环体 | |
} | |
for (var i = 0; i < a.length; i++) { | |
if(a[i] === undefined) continue; // 跳过undefined 和不存在的元素 | |
// 循环体 | |
} | |
for (var i = 0; i < a.length; i++) { | |
if(!(i in a)) continue; // 跳过不存在的元素 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function tail(o){ // 返回链表的最后一个节点对象 | |
for(; o.next; o=o.next) /* empty */ ; // 根据判断o.next 是不是真值来执行遍历 | |
return o; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 下面这些比较结果均是false | |
null == undefined // 这两值被认为相等 | |
"0" == 0 // 在比较之前字符串转换为数字 | |
0 == false // 在比较之前布尔值转换为数字 | |
"0" == false // 在比较之前字符串和布尔值都转换程数字 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 声明全局变量Set,使用一个函数的返回值给它赋值 | |
// 函数结束时紧跟的一对圆括号说明这个函数定义后立即执行 | |
// 它的返回值将赋值给Set,而不是将这个函数赋值给Set | |
// 注意它是一个函数表达式,而不是一条语句,因此函数“invocation” 并没有创建全局变量 | |
var Set = (function invocation(){ | |
function Set(){ // 这个构造函数是局部变量 | |
this.values = {} // 这个对象的属性用来保存这个集合 | |
this.n = 0 // 集合中值的个数 | |
this.add.apply(this, arguments) // 将所有的参数都添加到集合中 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 将o 的指定名字(或所有)的属性设置为不可写的和不可配置的 | |
function freezeProps(o) { | |
var props = arguments.length == 1 // 如果只有一个参数 | |
? Object.getOwnPropertyNames(o) // 使用所有的属性 | |
: Array.proptotype.splice.call(arguments, 1) // 否则传入了指定名字的属性 | |
props.forEach(function(n){ // 将它们都设置为只读的和不可变的 | |
// 忽略不可配置的属性 | |
if(!Objet.getOwnPropertyDescriptor(o, n).configurable) return | |
Object.defineProperty(o, n, { writable: false, configurable: false}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 将代码包装在一个匿名函数中,这样定义的变量就在这个函数作用域内 | |
(function(){ | |
//定义一个不可枚举的属性objectId,它可以被所有对象继承 | |
//当读取这个属性时调用getter函数 | |
//它没有定义setter,因此它是只读的 | |
//它是不可配置的,因此它是不能删除的 | |
Object.defineProperty(Object.prototype, "objectId", { | |
get: idGetter, // 取值器 | |
enumerable: false, // 不可枚举的 | |
configurable: false // 不可删除的 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 返回函数的名字,如果它有(非标准的)name 属性,则直接使用name 属性 | |
// 否则,将函数转换为字符串然后从中提取名字 | |
// 如果是没有名字的函数,则返回一个空字符串 | |
Function.prototype.getName = function(){ | |
return this.name || this.toString().match(/function\s*([^(]*)\(/)[1] | |
} |