Size: 237
Comment:
|
Size: 4857
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
= Javascript regular expression = | ## page was renamed from JavascriptRegularExpression = Javascript = |
Line 6: | Line 7: |
{{{ | {{{#!highlight javascript |
Line 11: | Line 12: |
== Delete property from object == {{{#!highlight javascript delete obj['propertyName']; }}} == Number of milliseconds since epoch == {{{#!highlight javascript var nrmillis = Date.now(); }}} == Foreach in array == https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach {{{#!highlight javascript 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 }}} == Context with this and bind == https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. {{{#!highlight javascript /*Constructor function*/ function Counter(){ this.counter=0; } Counter.prototype.run=function(){ console.log('counter:',this.counter); this.counter++; //recall run setTimeout(this.run.bind(this),1000); }; var c = new Counter(); setTimeout( c.run.bind(c) ,1000); // apply context of c to this }}} {{{#!highlight javascript function Counter(name,timeout){ this.counter=0; this.name=name; this.timeout=timeout; } Counter.prototype.run=function(){ console.log('counter:',this.counter,' name:',this.name,' timeout:',this.timeout); this.counter++; //recall run setTimeout(this.run.bind(this),this.timeout); }; var c = new Counter('c1',1000); setTimeout( c.run.bind(c) ,1000); var d = new Counter('c2',1500); setTimeout( d.run.bind(d) ,1500); }}} Two functions manipulate 2 JSON objects passed by '''this''' context. {{{#!highlight javascript function jsonFactory(c1,c2){ return {'counter1':c1,'counter2':c2}; } var data=jsonFactory(0,0); var data2=jsonFactory(100,100); function incCounter1(){ this.counter1++; } function incCounter2(){ this.counter2++; } setInterval(incCounter1.bind(data),1000); setInterval(incCounter2.bind(data),1000); setInterval(incCounter1.bind(data2),1000); setInterval(incCounter2.bind(data2),1000); }}} == arguments == https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments An Array-like object corresponding to the arguments passed to a function. You can refer to a function's arguments within the function by using the arguments object. This object contains an entry for each argument passed to the function, the first entry's index starting at 0. == bind, JSON and arguments sample == {{{#!highlight javascript #!/usr/bin/js // SpiderMonkey JavaScript example code // simulate console.log in SpiderMonkey var console={ log: function(msg) { //putstr( msg + "\n"); print( msg ); } }; function add(){ if(this.hasOwnProperty("op1") ) { console.log("op1 ..." + (this.op1 + this.op2) ); } if(this.hasOwnProperty("op3") ) { console.log("op3 ..." + (this['op3'] + this['op4']) ); } if(arguments.length>0) { console.log( arguments[0] ); } } //--------------------------------------- console.log("JSON sample ...."); var strx='{"ds":1122 , "hg":"ccvbn" }'; var objx = JSON.parse( strx ); console.log( objx.ds ); console.log( objx.hg ); objx.ds = objx.ds*2; console.log( objx.ds ); console.log( JSON.stringify( objx ) ); //--------------------------------------- console.log("bind sample"); var x={"op1":10, "op2":2}; var y={"op3":20, "op4":30}; var z={"op1":10,"op2":13}; var xxx = add.bind(x); // sets the this context with x object xxx("Arg test") // calls add with an argument var zzz = add.bind(y); // sets this context with y object zzz(); // calls add add.call(z); // sets this context and calls the function add }}} == Iterate keys in JSON object == {{{#!highlight javascript for (var key in response.result) { if (response.result.hasOwnProperty(key)) { } } }}} == Static properties and methods == http://elegantcode.com/2011/01/19/basic-javascript-part-7-static-properties-and-methods/ {{{#!highlight javascript function Podcast() {}; Podcast.FILE_EXTENSION = 'mp3'; // static const // static method Podcast.download = function(podcast) { console.log('Downloading ' + podcast + ' ...'); }; // instance method Podcast.prototype.play = function() { console.log('Playing this podcast ...'); }; }}} |
Javascript
Create an regular expression object
Replace string 'aaa' by 'xyz'.
Delete property from object
1 delete obj['propertyName'];
Number of milliseconds since epoch
1 var nrmillis = Date.now();
Foreach in array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
Context with this and bind
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
1 /*Constructor function*/
2 function Counter(){
3 this.counter=0;
4 }
5
6 Counter.prototype.run=function(){
7 console.log('counter:',this.counter);
8 this.counter++;
9 //recall run
10 setTimeout(this.run.bind(this),1000);
11 };
12
13 var c = new Counter();
14 setTimeout( c.run.bind(c) ,1000); // apply context of c to this
15
1 function Counter(name,timeout){
2 this.counter=0;
3 this.name=name;
4 this.timeout=timeout;
5 }
6
7 Counter.prototype.run=function(){
8 console.log('counter:',this.counter,' name:',this.name,' timeout:',this.timeout);
9 this.counter++;
10 //recall run
11 setTimeout(this.run.bind(this),this.timeout);
12 };
13
14 var c = new Counter('c1',1000);
15 setTimeout( c.run.bind(c) ,1000);
16 var d = new Counter('c2',1500);
17 setTimeout( d.run.bind(d) ,1500);
Two functions manipulate 2 JSON objects passed by this context.
1 function jsonFactory(c1,c2){
2 return {'counter1':c1,'counter2':c2};
3 }
4
5 var data=jsonFactory(0,0);
6 var data2=jsonFactory(100,100);
7
8 function incCounter1(){
9 this.counter1++;
10 }
11
12 function incCounter2(){
13 this.counter2++;
14 }
15
16 setInterval(incCounter1.bind(data),1000);
17 setInterval(incCounter2.bind(data),1000);
18 setInterval(incCounter1.bind(data2),1000);
19 setInterval(incCounter2.bind(data2),1000);
arguments
An Array-like object corresponding to the arguments passed to a function.
You can refer to a function's arguments within the function by using the arguments object. This object contains an entry for each argument passed to the function, the first entry's index starting at 0.
bind, JSON and arguments sample
1 #!/usr/bin/js
2 // SpiderMonkey JavaScript example code
3 // simulate console.log in SpiderMonkey
4 var console={
5 log: function(msg) {
6 //putstr( msg + "\n");
7 print( msg );
8 }
9 };
10
11 function add(){
12 if(this.hasOwnProperty("op1") ) {
13 console.log("op1 ..." + (this.op1 + this.op2) );
14 }
15
16 if(this.hasOwnProperty("op3") ) {
17 console.log("op3 ..." + (this['op3'] + this['op4']) );
18 }
19
20 if(arguments.length>0) {
21 console.log( arguments[0] );
22 }
23 }
24
25 //---------------------------------------
26 console.log("JSON sample ....");
27 var strx='{"ds":1122 , "hg":"ccvbn" }';
28 var objx = JSON.parse( strx );
29 console.log( objx.ds );
30 console.log( objx.hg );
31 objx.ds = objx.ds*2;
32 console.log( objx.ds );
33 console.log( JSON.stringify( objx ) );
34
35 //---------------------------------------
36 console.log("bind sample");
37 var x={"op1":10, "op2":2};
38 var y={"op3":20, "op4":30};
39 var z={"op1":10,"op2":13};
40 var xxx = add.bind(x); // sets the this context with x object
41 xxx("Arg test") // calls add with an argument
42 var zzz = add.bind(y); // sets this context with y object
43 zzz(); // calls add
44 add.call(z); // sets this context and calls the function add
45
Iterate keys in JSON object
Static properties and methods
http://elegantcode.com/2011/01/19/basic-javascript-part-7-static-properties-and-methods/
1 function Podcast() {};
2
3 Podcast.FILE_EXTENSION = 'mp3'; // static const
4
5 // static method
6 Podcast.download = function(podcast) {
7 console.log('Downloading ' + podcast + ' ...');
8 };
9
10 // instance method
11 Podcast.prototype.play = function() {
12 console.log('Playing this podcast ...');
13 };