随笔分类 -  JavaScript

摘要:• undefined means “no value” (neither primitive nor object). Uninitialized variables,missing parameters, and missing properties have that nonvalue. And functionsimplicitly return it if nothing has been explicitly returned.• null means “no object.” It is used as a nonvalue where an object is expecte. 阅读全文
posted @ 2014-04-12 15:14 LambdaTea 阅读(221) 评论(0) 推荐(0)
摘要:'use strict';var jane = { name: 'Jane', describe: function() { return 'Person named ' + this.name; }};var func = jane.describe;func(); //TypeError: Cannot read property 'name' of undefinedvar func2 = jane.describe.bind(jane); JS里的bind概念基本和C++是一样的,要有对象。 阅读全文
posted @ 2014-04-12 13:48 LambdaTea 阅读(507) 评论(0) 推荐(0)
摘要://模拟类式继承//Douglas CrockfordFunction.prototype.method = function(name,func) { this.prototype[name] = func; return this;};Function.method('inherits', function(parent) { var depth = 0; var proto = this.prototype = new parent(); this.method('uber', function uber(name)) { var func; ... 阅读全文
posted @ 2013-10-26 15:33 LambdaTea 阅读(155) 评论(0) 推荐(0)
摘要:感觉就是C++中的 functor+bind.http://stackoverflow.com/questions/5176313/javascript-curry// define the curry() functionfunction curry(fn, scope) { // set the scope to window (the default global object) if no scope was passed in. scope = scope || window; // Convert arguments into a plain array, bec... 阅读全文
posted @ 2013-10-25 17:18 LambdaTea 阅读(448) 评论(0) 推荐(0)
摘要:/* range.js */function range(from, to) { var r = inherit(range.methods); r.from = from; r.to = to; return r;}range.methods = { includes: function(x) { return this.from <= x && x <= this.to; }, foreach: function(f) { for (var x=Math.ceil(this.from); x<=this.to; x++)... 阅读全文
posted @ 2013-10-19 15:42 LambdaTea 阅读(192) 评论(0) 推荐(0)
摘要:var strict = (function() { return !this;}());//function getPropertyNames(o, /* optional */ a) { if (a === undefined) a = []; /* a = a || [] */ for(var property in o) { a.push(property); } return a;}/* callee 当前正在执行的函数, caller 当前正在执行的函数的函数 *///var scope = "global scope";function... 阅读全文
posted @ 2013-10-19 15:05 LambdaTea 阅读(192) 评论(0) 推荐(0)
摘要:/* array */var empty = [];var primes = [2,3,5];var misc = [1.1, true, "a"];//var a = new Array(10);//a = [1,2,3];Object.defineProperty(a, "length", {writable: false});a.length = 0; //a will not changevar keys = Object.keys(o); //获得o对象属性名组成的数组var values = [];for(var i = 0; i<ke 阅读全文
posted @ 2013-10-19 14:39 LambdaTea 阅读(137) 评论(0) 推荐(0)
摘要:var o3 = Object.create(Object.prototype); //equals new Object()//function inherit(p) { if(p==null) throw TypeError(); if(Object.create) { return Object.create(p); } var t = typeof p; if(t!=="object" && t!=="function") throw TypeError(); function f() {}; f.prototype = p; . 阅读全文
posted @ 2013-10-18 19:43 LambdaTea 阅读(209) 评论(0) 推荐(0)
摘要:1 //object 2 var book = { 3 topic: "JavaScript", 4 fat : true 5 }; 6 book.topic = "CPP"; 7 8 //array 9 var primes = [2,3,5,7];10 primes.length11 12 //13 var points = [14 {x:0, y:0},15 {x:1, y:1}16 ];17 var data = {18 t... 阅读全文
posted @ 2013-10-18 17:23 LambdaTea 阅读(163) 评论(0) 推荐(0)
摘要:function decimalToHexString(number) { if (number < 0) { number = 0xFFFFFFFF + number + 1; } return number.toString(16).toUpperCase();} 阅读全文
posted @ 2013-07-02 15:54 LambdaTea 阅读(379) 评论(0) 推荐(0)