Size: 237
Comment:
|
Size: 1533
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 }}} |
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