node.js支持javascript 1.8.5,有一些新的特性和api,罗列下,可以方便我们写程序:
Javascript 1.8.5新特性,直接上新api和详细介绍地址
这些特性在浏览器FF8.0我都简单测试了通过,大部分需要ie9.0以上才能很好支持,还是给node.js用吧。
1、 Object.create
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create
相当于对象复制,不能完全替代new func().
2、 Object.defineProperty
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperty
定义属性,现在可以定义一些私有,只读属性了
##3、Object.defineProperties
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperties
批量定义属性,例子:1
2
3
4
5
6
7
8
9
10Object.defineProperties(my_dog, {
age : {
get : function () { /* . . . */ },
set : function () { /* . . . */ },
enumerable: true
},
gender : {
value : "female"
}
})
##4、Object.getOwnPropertyDescriptor
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor
获得指定对象定义的属性对象,比如是否可以枚举,是否可写,返回object
5、Object.keys
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys
返回数组,该对象的属性key名,不列出原型链上的属性和不可枚举属性1
2var horse = { name : "Ed", age : 4, job : "jumping", owner : "Jim" };
var horse_keys = Object.keys(horse); // ["name", "age", "job", "owner"]
6、Object.getOwnPropertyNames
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
This one is just like Object.keys, except that it includes all the properties—even the ones that aren’t enumerable. By the longer function name, you can tell they discourage the use of it. Usually, you’ll want keys instead.
加强版的Object.keys,列出所有属性,但是不列出原型链的,列出定义时不可枚举的属性
7、Object.preventExtensions / Object.isExtensible
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/preventExtensions
不能对此对象进行添加新属性,但是可以修改和删除原有属性,并且可以在这个对象的原型链上添加新属性。1
2
3
4
5
6var product = { name : "Foobar", rating : 3.5 };
Object.isExtensible(product); // true
Object.preventExtensions(product);
Object.isExtensible(product); // false
product.price = "$10.00"; // doesn't work
product.price; // undefined
8、 Object.seal / Object.isSealed
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/seal
上面方法的加强版,可以修改,但是尝试将数据属性修改为访问器或者反之,或者删除,或者增加都将失败。1
2
3
4
5
6var pet = { name : "Browser", type : "dog" };
Object.seal(pet);
pet.name = "Oreo";
pet.age = 2; // doesn't work
pet.type = function () { /**/ }; // doesn't work
delete pet.name; // doesn't work
9、Object.freeze / Object.isFrozen
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/freeze
最强劲的加锁版,只读版本。1
2
3var obj = { greeting : "Hi!" };
Object.freeze(obj);
Object.isFrozen(obj); // true
10、Array.isArray
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray1
2var names = ["Collis", "Cyan"];
Array.isArray(names); // true
11、Date.prototype.toJSON
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/toJSON1
new Date().toJSON(); // "2010-12-06T16:25:40.040Z"
12、Function.prototype.bind
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
有点像call和apply,绑定this环境变量1
2
3
4
5
6
7
8
9
10var tooltip = { text: "Click here to . . . " },
overlay = { text: "Please enter the number of attendees" };
function show_text () {
// really, do something more useful here
console.log(this.text);
}
tooltip.show = show_text.bind(tooltip);
tooltip.show();
overlay.show = show_text.bind(overlay);
overlay.show();
13、Object.getPrototypeOf
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/getPrototypeOf
获得指定对象的原型链对象1
2
3
4var x = function(){}
x.prototype.n = function(){}
var y = new x()
console.log(Object.getPrototypeOf(y))
14、String.prototype.trim、trimLeft()、trimRight()
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/trim
呼之欲出的trim方法,再也不用自己写了1
2var orig = " foo ";
alert(orig.trim())
15、Array.prototype.indexOf
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf1
2
3
4
5indexOf方法的数组版本
array.indexOf(searchElement[, fromIndex])
var a = [1,2,3,4,5]
console.log(a.indexOf(5))
console.log(a.indexOf(7))
16、Array.prototype.lastIndexOf
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf
17、Array.prototype.every
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
对数组每一个元素进行匹配,最后返回结果的 “与” 结果。1
2
3
4
5
6
7function isBigEnough(element, index, array) {
return (element >= 10);
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
// passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough);
// passed is true
18、Array.prototype.some
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
对数组每一个元素进行匹配,最后返回结果的 “或” 结果。1
2
3
4
5
6
7function isBigEnough(element, index, array) {
return (element >= 10);
}
var passed = [2, 5, 8, 1, 4].some(isBigEnough);
// passed is false
passed = [12, 5, 8, 1, 4].some(isBigEnough);
// passed is true
19、Array.prototype.forEach
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach
用的最多的方法,看例子吧:1
2
3
4
5
6
7
8function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element);
}
[2, 5, 9].forEach(logArrayElements);
// logs:
// a[0] = 2
// a[1] = 5
// a[2] = 9
20、Array.prototype.map
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map
如果想批量替换数组中的某些值,用这个吧1
2
3
4
5
6
7
8function fuzzyPlural(single) {
return single.replace(/o/g, 'e');
}
var words = ["foot", "goose", "moose"];
console.log(words.map(fuzzyPlural));
// ["feet", "geese", "meese"]
console.log(words)
//["foot", "goose", "moose"]
21、Array.prototype.filter
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
返回过滤器中return true的元素,生成新的数组:1
2
3
4
5function isBigEnough(element, index, array) {
return (element >= 10);
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]
22、Array.prototype.reduce
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduce
从左到右以此,a表示上一个运算结果或第一个元素,b表示下一个元素1
2
3
4
5
6
7
8var total = [0, 1, 2, 3].reduce(function(a, b) {
return a + b;
});
// total == 6
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {
return a.concat(b);
});
// flattened is [0, 1, 2, 3, 4, 5]
23、Array.prototype.reduceRight
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/ReduceRight
上面那个反过来1
2
3
4
5
6
7
8
9var total = [0, 1, 2, 3].reduceRight(function(a, b) {
return a + b;
});
// total == 6
var flattened = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) {
return a.concat(b);
}, []);
// flattened is [4, 5, 2, 3, 0, 1]
24、fun.isGenerator()
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/isGenerator
函数是否生成,看例子一目了然:1
2
3
4
5
6
7
8function f() {}
function g() {
yield 42;
}
console.log("f.isGenerator() = " + f.isGenerator());
console.log("g.isGenerator() = " + g.isGenerator()); .
//f.isGenerator() = false
//g.isGenerator() = true
25、JSON.parse
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON/parse
将字符串变为json,不知道这里为什么要加个k===””判断,我试过了,不加这个会报错,不解啊!1
2var transformed = JSON.parse('{"p": 5}', function(k, v) { alert(k);if (k === "") return v; return v * 2; });
console.log(transformed )
26、JSON.stringify(value[, replacer [, space]])
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON/stringify
https://developer.mozilla.org/En/Using_native_JSON#The_replacer_parameter
将对象转换成字符串,node.js中可以用来发送给php或者给前端,可以加一些过滤条件,有function和array1
2
3
4
5
6JSON.stringify({ uno: 1, dos : 2 }, null, '\t')
// returns the string:
// '{ \
// "uno": 1, \
// "dos": 2 \
// }'
过滤条件为function,比较麻烦,仔细看吧:1
2
3
4
5
6
7
8
9
10
11
12
13
14function censor(key, value) {
if (typeof(value) == "string") {
return undefined;
}
return value;
}
var foo = {foundation: "Mozilla", model: "box", week: 45, transport: "car", month: 7};
var jsonString = JSON.stringify(foo, censor);
If you return a Number, the string corresponding to that number is used as the value for the property when added to the JSON string.
If you return a String, that string is used as the property's value when adding it to the JSON string.
If you return a Boolean, "true" or "false" is used as the property's value, as appropriate, when adding it to the JSON string.
If you return any other object, the object is recursively stringified into the JSON string, calling the replacer function on each property, unless the object is a
function, in which case nothing is added to the JSON string.
If you return undefined, the property is not included in the output JSON string.
过滤条件为数组,则只输出指定的key名为json字符串:1
2
3
4var x = {a:123, b:456}
var z = JSON.stringify(x, ['a'],'\t')
alert(z)
//'{"a":123}'
28、get和set
https://developer.mozilla.org/en/JavaScript/Reference/Operators/get
https://developer.mozilla.org/en/JavaScript/Reference/Operators/set
29、defineSetter和defineGetter
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineSetter
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineGetter
更多:
https://developer.mozilla.org/en/JavaScript/Reference#About_this_Reference