Skip to content

Instantly share code, notes, and snippets.

View karenpeng's full-sized avatar
🏠
Working from home

Karen Peng karenpeng

🏠
Working from home
View GitHub Profile
function Robot(){
this.inside = []
this.a = 0
}
Robot.prototype.outside = []
Robot.prototype.b = 0
Robot.prototype.test = function(){
console.log(this.b)
}
function Parent () {
this.arrayInside = [1];
this.numberInside = 1;
}
Parent.arrayOutside = [2];
Parent.numberOutside = 2;
Parent.prototype.arrayPrototype = [3];
Parent.prototype.numberPrototype = 3;
// class A
function ParentA(a) {
this.a = a;
};
ParentA.prototype.displayA = function () {
console.log('a: %s', this.a);
}
// class B
function ParentB(b) {
function block(){
var lock = false
return function dosomething(){
if(!lock){
console.log('coo')
lock = true
}
setTimeout(function(){
lock = false
@karenpeng
karenpeng / Object Flatten.js
Last active October 1, 2015 01:00 — forked from penguinboy/Object Flatten
Flatten javascript objects into a single-depth object
var flattenObject = function(ob) {
var toReturn = {};
for (var i in ob) {
if (!ob.hasOwnProperty(i)) continue;
if ((typeof ob[i]) == 'object') {
var flatObject = flattenObject(ob[i]);
for (var x in flatObject) {
if (!flatObject.hasOwnProperty(x)) continue;
@karenpeng
karenpeng / _.md
Created August 21, 2015 15:21
test
var Mocha = require('mocha');
var assert = require('assert');
var mocha = new Mocha({ui: 'bdd'});
/*
Your previous Plain Text content is preserved below:
// 实现一个函数 sequence,参数是一个数组,是一系列的异步函数,返回一个新的函数,这个新的函数在调用的时候,会顺序的执行之前传递给 sequence 的函数数组,前一个函数的返回值作为下一个函数的参数,新函数的参数前面部分会传递给函数数组的第一个函数,当全部执行完成后,调用 新函数的 callback。
sequence([add1, add2])(1, function (err, res) {
function parallel(arrOfFunc, maxNum, callback){
var len = arrOfFunc.length
var result = []
// 最好是增加一个标记
var done = false
var lastIndex = maxNum -1
for(var i = 0; i < maxNum; i++){
// wrap 为什么要返回一个函数然后执行?直接在 wrap 里面执行就可以了
wrap(i, arrOfFunc[i])
function iterator(arr){
var id = setInterval(
function(){
console.log('haha')
if(arr.length > 0){
console.log(arr.shift())
}else{
clearInterval(id)
for(var i = 0; i < 5; i++){
setTimeout(function(){
//console.log(i)
}, i * 1000)
}
for(var i = 0; i < 5; i++){
(function(i){
setTimeout(function(){
console.log(i)