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 < 5; i++){ | |
(function(i){ | |
setTimeout(function(){ | |
console.log(i) | |
}, 1e3*i) | |
}(i)) | |
} |
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 mongoose = require('mongoose'); | |
mongoose.connect('mongodb://localhost/test'); | |
var db = mongoose.connection; | |
db.on('error', console.error.bind(console, 'connection error:')) | |
db.once('open', function(){ | |
var catSchema = mongoose.Schema({ | |
name: String, |
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
// inherit() 返回一个继承自原型对象p 的属性的新对象 | |
// 这里使用ECMAScript 5 中的Object.create() 函数(如果存在的话) | |
// 如果不存在Object.create(),则退化使用其他方法 | |
function inherit(p) { | |
if(p == null) throw TypeError(); // p 是一个对象,但不能是null | |
if(Object.create) // 如果Object.create() 存在 | |
return Object.create(p) // 直接使用它 | |
var t = typeof p // 否则进行进一步检测 | |
if(t !== "object" && t !== "function") throw TypeError(); | |
function f() {} // 定义一个空构造函数 |
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
/* | |
* 将p 中的可枚举属性复制至o 中,并返回o | |
* 如果o 和p 有同名的属性,则覆盖o 中的属性 | |
* 这个函数并不处理getter 和setter 以及复制属性 | |
*/ | |
function extend(o, p) { | |
for(prop in p){ // 遍历p 中的所有属性 | |
o[prop] = p[prop] // 将属性添加至o 中 | |
} | |
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
/* | |
* 给Object.prototype 添加一个不可枚举的extend 方法 | |
* 这个方法继承自调用它的对象,将作为参数参入的对象的属性一一复制 | |
* 除了值之外,也复制属性的所有特性,除非在目标对象中存在同名的属性, | |
* 参数对象的所有自有对象(包括不可枚举的属性)也会一一复制。 | |
*/ | |
Object.defineProperty(Object.prototype, | |
"extend", // 定义Object.prototype.extend | |
{ | |
writable: true, |
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 classof(o){ | |
if(o === null) return 'Null' | |
if(o === undefined) return 'Undefined' | |
return Object.prototype.toString.call(o).slice(8, -1) | |
} |
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
/* | |
* Complex.js | |
* 这个文件定义了Complex 类,用来描述复数 | |
* 回忆一下,复数是实数和虚数的和,并且虚数i 是-1 的平方根 | |
*/ | |
/* | |
* 这个构造函数为它所创建的每个实例定义了实例字段r和i | |
* 这两个字段分别保存复数的实部和虚部 | |
* 它们是对象的状态 | |
*/ |
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] | |
} |
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
// 将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}) | |
}) |
OlderNewer