splice(start-index, length, inject-element-01, inject-element-02, ...) The first two parameters define the start index and length (number of elements) of the slice to be removed; the other parameters pass the new values:。modifies the source array。
splice(start-index, length, inject-element-01, inject-element-02, ...) The first two parameters define the start index and length (number of elements) of the slice to be removed; the other parameters pass the new values:。modifies the source array。返回一个新数组,由被替换的元素组成。
varninja={name:'Ninja',say:function(){return'I am a '+this.name;}};>functionF(){}>typeofF.prototype;"object">F.prototype=ninja;// 修改构造函数的 prototype 属性,构造出新类型的对象>varbaby_ninja=newF();>baby_ninja.name;"Ninja">baby_ninja.say();"I am a Ninja"
toString
toString() 返回函数定义代码
1
2
3
4
5
6
7
8
9
10
11
12
13
>functionmyfunc(a,b,c){returna+b+c;}>myfunc.toString();"function myfunc(a, b, c) {
return a + b + c;
}"// 内建函数打印不出来代码,只能得到 native code 的提示。>parseInt.toString();"function parseInt() { [native code] }"
varsome_obj={name:'Ninja',say:function(who){return'Haya '+who+', I am a '+this.name;}};>some_obj.say('Dude');"Haya Dude, I am a Ninja">varmy_obj={name:'Scripting guru'};>some_obj.say.call(my_obj,'Dude');// 以 my_obj 为 this,执行 some_obj.say"Haya Dude, I am a Scripting guru"// 用 call 调用多个参数的方法some_obj.someMethod.call(my_obj,'a','b','c');// apply vs callsome_obj.someMethod.apply(my_obj,['a','b','c']);some_obj.someMethod.call(my_obj,'a','b','c');
Math 和其他 built-in 对象不同, 不是构造方法,是一个全局对象,包含使用的常量和方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// The constant π:>Math.PI;3.141592653589793// Square root of 2:>Math.SQRT2;1.4142135623730951// Euler's constant:>Math.E;2.718281828459045// Natural logarithm of 2:>Math.LN2;0.6931471805599453// Natural logarithm of 10:>Math.LN10;2.302585092994046
random() function returns a number between 0 and 1
1
2
>Math.random();0.3649461670235814
For numbers between any two values, use the formula ((max - min) * Math.random()) + min .
1
2
3
// 2-10的随机数>8*Math.random()+2;9.175650496668485
小数转整数,floor, ceil, round
floor() to round down
ceil() to round up
round() to round to the nearest
min, max 比较并返回结果
数学计算,乘方、开方、三角函数
raise to a power using pow()
find the square root using sqrt()
trigonometric operations—sin(), cos(), atan()
Date
new Date
今天
1
2
>newDate();WedFeb27201323:49:28GMT-0800(PST)
指定日期&时间字符串
1
2
3
4
5
6
>newDate('2015 11 12');ThuNov12201500:00:00GMT-0800(PST)>newDate('1 1 2016');FriJan01201600:00:00GMT-0800(PST)>newDate('1 mar 2016 5:30');TueMar01201605:30:00GMT-0800(PST)
The rest of the parameters contain any strings matched by any groups in your regex pattern
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>varglob;>varre=/(.*)@(.*)\.(.*)/;varcallback=function(){glob=arguments;returnarguments[1]+' at '+arguments[2]+' dot '+arguments[3];};>"stoyan@phpied.com".replace(re,callback);"stoyan at phpied dot com">glob;["stoyan@phpied.com","stoyan","phpied","com",0,"stoyan@phpied.com"]
try{vartotal=maybeExists();if(total===0){thrownewError('Division by zero!');}else{alert(50/total);}}catch(e){alert(e.name+': '+e.message);}finally{alert('Finally!');}
直接抛出 object
1
2
3
4
throw{name:"MyError",message:"OMG! Something terrible has happened"}