一些JavaScript 1.6~1.8上的高级技巧~
2007-09-04 12:45 乱世文章 阅读(144) 评论(0) 编辑 收藏 举报在jslibs的项目wiki上看到这些技巧,都是一些在mozilla spidermonkey javascript上的高级戏法。在JScript上不能用的哈~。这里转过来,很多技巧挺有趣的。
原文引用在:
http://code.google.com/p/jslibs/wiki/JavascriptTips
------------------------------------------
1. destructuring assignments
JavaScript 1.7
var{a:x}={a:7};
Print(x);// prints: 7
Print(x);// prints: 7
2. Generator Expressions
JavaScript 1.7
[ y for( y in[5,6,7,8,9])]// is [0,1,2,3,4]
and
[ y for each ( y in[5,6,7,8,9])]// is [5,6,7,8,9]
Because in for extracts index names, and for each extracts the values.
3. Advanced use of iterators
JavaScript 1.8
Number.prototype.__iterator__ =function(){
for( let i =0; i <this; i++)
yield i;
};
for( let i in5)
print(i);
for( let i =0; i <this; i++)
yield i;
};
for( let i in5)
print(i);
prints:
1
2
3
4
5
2
3
4
5
This make Number object to act as a generator.
4. Expression Closures
JavaScript 1.8
function(x) x * x;
Note that braces {...} and return are implicit
5. Basic debugging
JavaScript 1.6
LoadModule('jsstd');
functionThrowAnError(){
(function(){
thrownewError("Whoops!");
})();
}
try{
ThrowAnError()
}catch(e){
Print(e.stack);
}
functionThrowAnError(){
(function(){
thrownewError("Whoops!");
})();
}
try{
ThrowAnError()
}catch(e){
Print(e.stack);
}
prints:
Error("Whoops!")@:0
()@test.js:7
ThrowAnError()@test.js:5
@test.js:14
()@test.js:7
ThrowAnError()@test.js:5
@test.js:14
6. Multiple-value returns
JavaScript 1.7
function f(){
return[1,2];
}
var[a, b]= f();
Print( a +' '+ b );// prints: 1 2
return[1,2];
}
var[a, b]= f();
Print( a +' '+ b );// prints: 1 2
7. Operator [ ] and strings ( like charAt() )
JavaScript 1.6
var str ='foobar';
Print( str[4]);
Print( str[4]);
prints:
a
8. indexOf() and lastIndexOf() Works on Array
JavaScript 1.6
var obj ={};
var arr =['foo',567, obj,12.34];
Print( arr.indexOf(obj));// prints: 2
var arr =['foo',567, obj,12.34];
Print( arr.indexOf(obj));// prints: 2
9. Using Array functions on a non-Array object
JavaScript 1.7
var obj ={};
Array.push(obj,'foo');
Array.push(obj,123);
Array.push(obj,5.55);
Print( obj.toSource());// prints: ({0:"foo", length:3, 1:123, 2:5.55})
Array.push(obj,'foo');
Array.push(obj,123);
Array.push(obj,5.55);
Print( obj.toSource());// prints: ({0:"foo", length:3, 1:123, 2:5.55})
10. Simulate threads using yield operator
JavaScript 1.7
var scheduler =newfunction(){
var _workers =[];
this.Add=function( worker ){
_workers.push(new worker());
}
this.Run=function(){
while( _workers.length )
for each (var worker in _workers )
try{
worker.next();
}catch(err if err instanceofStopIteration){
_workers.splice( _workers.indexOf(worker),1);
}
}
}
function worker1(){
for(var i =0; i <5; i++){
Print('worker1: '+i,'/n');
yield;
}
}
scheduler.Add(worker1);
function worker2(){
for(var i =0; i <10; i++){
Print('worker2: '+i,'/n');
yield;
}
}
scheduler.Add(worker2);
scheduler.Run();
var _workers =[];
this.Add=function( worker ){
_workers.push(new worker());
}
this.Run=function(){
while( _workers.length )
for each (var worker in _workers )
try{
worker.next();
}catch(err if err instanceofStopIteration){
_workers.splice( _workers.indexOf(worker),1);
}
}
}
function worker1(){
for(var i =0; i <5; i++){
Print('worker1: '+i,'/n');
yield;
}
}
scheduler.Add(worker1);
function worker2(){
for(var i =0; i <10; i++){
Print('worker2: '+i,'/n');
yield;
}
}
scheduler.Add(worker2);
scheduler.Run();
prints:
worker1:0
worker2:0
worker1:1
worker2:1
worker1:2
worker2:2
worker1:3
worker2:3
worker1:4
worker2:4
worker2:5
worker2:6
worker2:7
worker2:8
worker2:9
worker2:0
worker1:1
worker2:1
worker1:2
worker2:2
worker1:3
worker2:3
worker1:4
worker2:4
worker2:5
worker2:6
worker2:7
worker2:8
worker2:9
11. swap two variables
JavaScript 1.7
var a =1;
var b =2;
[a,b]=[b,a];
var b =2;
[a,b]=[b,a];
12. Destructuring assignment with function arguments
JavaScript 1.7
function foo([a,b]){
Print(a);
Print(b);
}
foo([12,34]);
Print(a);
Print(b);
}
foo([12,34]);
Prints:
12
34
34
13. JavaScript scope and LET instruction
JavaScript 1.7
var x =5;
var y =0;
let (x = x+10, y =12){
Print(x+y);
}
Print(x+y);
var y =0;
let (x = x+10, y =12){
Print(x+y);
}
Print(x+y);
prints:
27
5
5
or,
for( let i=0; i <10; i++){
Print(i +' ');
}
Print(i);
Print(i +' ');
}
Print(i);
prints:
0123456789 test.js:4:ReferenceError: i isnotdefined
14. Iterate on values
JavaScript 1.6
for each (var i in[3,23,4])
Print(i)
Print(i)
Prints:
3
23
4
23
4