javascript 1.8.5 新特性,新API

https://developer.mozilla.org/en/JavaScript/Reference#About_this_Reference

http://snoopyxdy.blog.163.com/blog/static/601174402011112841744756/

javascript 1.8.5 新特性,新API   

2011-12-28 16:49:39 |  分类: node |  标签: node.js   javascript新特新   javascript新api   javascript1.8   js1.8    | 字号   订阅

node.js支持javascript 1.8.5,有一些新的特性和api,罗列下,可以方便我们写程序:

Javascript 1.8.5新特性,直接上新api和详细介绍地址

这些特性在浏览器FF8.0我都简单测试了通过,大部分需要ie9.0以上才能很好支持,还是给node.js用吧。

1、 Object.create
相当于对象复制,不能完全替代new func().

2、 Object.defineProperty
定义属性,现在可以定义一些私有,只读属性了

3、Object.defineProperties
批量定义属性,例子:
Object.defineProperties(my_dog, {
    age : {
        get : function () { /* . . . */ },
        set : function () { /* . . . */ },
        enumerable: true
    },
    gender : {
        value : "female"
    }
})

4、Object.getOwnPropertyDescriptor
获得指定对象定义的属性对象,比如是否可以枚举,是否可写,返回object

5、Object.keys
返回数组,该对象的属性key名,不列出原型链上的属性和不可枚举属性
var horse = { name : "Ed", age : 4, job : "jumping", owner : "Jim" };
var horse_keys = Object.keys(horse); // ["name", "age", "job", "owner"]

6、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
不能对此对象进行添加新属性,但是可以修改和删除原有属性,并且可以在这个对象的原型链上添加新属性。
var 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
上面方法的加强版,可以修改,但是尝试将数据属性修改为访问器或者反之,或者删除,或者增加都将失败。
var 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
最强劲的加锁版,只读版本。
var obj = { greeting : "Hi!" };
Object.freeze(obj);
Object.isFrozen(obj); // true

10、Array.isArray
var names = ["Collis", "Cyan"];
Array.isArray(names); // true

11、Date.prototype.toJSON
new Date().toJSON(); // "2010-12-06T16:25:40.040Z"

12、Function.prototype.bind
有点像call和apply,绑定this环境变量
var 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
获得指定对象的原型链对象
var x = function(){}
x.prototype.n = function(){}
var y = new x()
console.log(Object.getPrototypeOf(y))

14、String.prototype.trim、trimLeft()、trimRight()
呼之欲出的trim方法,再也不用自己写了
var orig = "   foo  ";  
alert(orig.trim())

15、Array.prototype.indexOf
indexOf方法的数组版本
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

17、Array.prototype.every
对数组每一个元素进行匹配,最后返回结果的 “与” 结果。
function 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
对数组每一个元素进行匹配,最后返回结果的 “或” 结果。
function 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
用的最多的方法,看例子吧:
function 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
如果想批量替换数组中的某些值,用这个吧
function 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
返回过滤器中return true的元素,生成新的数组:
function 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
从左到右以此,a表示上一个运算结果或第一个元素,b表示下一个元素
var 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
上面那个反过来
var 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()
函数是否生成,看例子一目了然:
function 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
将字符串变为json,不知道这里为什么要加个k===""判断,我试过了,不加这个会报错,不解啊!
var 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]])
将对象转换成字符串,node.js中可以用来发送给php或者给前端,可以加一些过滤条件,有function和array
JSON.stringify({ uno: 1, dos : 2 }, null, '\t')  
// returns the string:  
// '{            \  
//     "uno": 1, \  
//     "dos": 2  \  
// }'  

过滤条件为function,比较麻烦,仔细看吧:
function 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字符串:
var x = {a:123, b:456}
var z = JSON.stringify(x, ['a'],'\t')
alert(z)
//'{"a":123}'


28、get和set


29、__defineSetter__和__defineGetter__


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值