## page was renamed from JavascriptRegularExpression = Javascript = ECMAScript 5.1 http://www.ecma-international.org/ecma-262/5.1/ECMA-262.pdf (ECMA-262 5.1 Edition / June 2011) ECMAScript 6.0 (2015) http://www.ecma-international.org/ecma-262/6.0/ECMA-262.pdf https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects == Create an regular expression object == Replace string 'aaa' by 'xyz'. {{{#!highlight javascript var stringx = 'aaa bbb ccc'; var regexx = new RegExp('aaa','g'); stringx = stringx.replace( regexx , 'xyz' ); }}} Extract date without certain characters {{{#!highlight javascript var currDate = new Date().toISOString(); currDate = currDate.replace( new RegExp('-','g') , '' ).replace( new RegExp(':','g') , '' ); currDate = currDate.substring(0,15); }}} == 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 ...'); }; }}} == ES6 ECMAScript 6 class example == {{{#!highlight javascript class P{ // syntax sugar for a constructor function for ES5 // in ES5 would be something like function P(a,b){} constructor(a,b){ this.a=a; this.b=b; // this continues to be context for a function } // in ES5 would be something like P.prototype.mul = function(){}; mul(){ return this.a*this.b; } // in ES5 would be something like P.sum=function(c,d){}; static sum(c,d){ return c+d; } // in ES5 would be something like p.mulres get mulres(){ return this.mul(); } } }}} == JSON pretty print == * http://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript {{{#!highlight javascript var str = JSON.stringify(obj, null, 2); }}} == Function based OOP == {{{#!highlight javascript // Constructor function function init(value) { // instance variable var value = value; function dec() { value--; } function inc() { value++; } function get() { return value; } // instance methods return { 'inc': inc, 'dec': dec, 'get': get }; } x = init(10); y = init(9); function other(x1, y1) { x1['inc'](); x1['inc'](); console.log(x1['get']()); console.log(x1['get']()); y1['dec'](); y1.dec(); console.log(y1['get']()); other2(x1, y1); } other(x,y); function other2(x2, y2) { x2['inc'](); x2['inc'](); console.log(x2['get']()); console.log(x2['get']()); y2['dec'](); y2.dec(); console.log(y2['get']()); } }}}