V8中的js

来源:互联网 发布:psp合盘软件 编辑:程序博客网 时间:2024/05/29 08:00

Object.keys()

//通常js获取对象的键的方法,避免取到dvar a = {a:'b',c:'d'};Object.prototype.d = 'f';for(var i in a){    if(a.hasOwnProperty(i)){        console.log(a[i]);    }}//V8中提供Object.keys(a);Object.keys(a);//['a','c']

Array.isArray()

Array.isArray([]);//true

数组主要方法
遍历数组,forEach(类似于jQuery的$.each)

[1,2,3].forEach(function(v){    console.log(v);})

过滤数组,filter(类似jQuery的$.grep)

[1,2,3].filter(function(v){    return v<3;})//返回[1,2]

改变数组中某个元素的值,map(类似jQuery的$.map)

[5,10,15].map(function(v){    return v *2;})//返回[10,20,30]

字符串方法
移除首末空格,trim()

'    hello    '.trim();// 'hello'

JSON、String转换
JSON.prase(),JSON.stringify()

function.bind
.bind(类似于jQuery的$.proxy)允许改变对this的引用

function a (){     this.hello == 'world';//true};var b = a.bind({hello:'world'});b();

proto 继承

function Animal(){};function Ferret(){};Ferret.prototype._proto_ = Animal.prototype;

存取器
访问属性用defineGetter,设置属性用defineSetter

原创粉丝点击