## page was renamed from JavascriptRegularExpression
= Javascript =

== 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' );
}}}

== 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);
}}}

== 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 ...');
};
}}}