javascript cheatsheet
Javascript cheatsheet
DateTime manipulation
new Date().toISOString().substr(0, 10)
"2020-11-07"
var d = new Date("2015-03-25");
undefined
d;
Wed Mar 25 2015 05:30:00 GMT+0530 (India Standard Time)
var d = new Date("2020-11-05T06:04:03Z");
undefined
d;
Thu Nov 05 2020 11:34:03 GMT+0530 (India Standard Time)
d.toISOString();
"2020-11-05T06:04:03.000Z"
d.toGMTString();
"Thu, 05 Nov 2020 06:04:03 GMT"
d.toDateString();
"Thu Nov 05 2020"
d;
Thu Nov 05 2020 11:34:03 GMT+0530 (India Standard Time)
d.toLocaleString();
"11/5/2020, 11:34:03 AM"
Dynamic Function
var x = new Function('a','console.log(a)');
x('hello');
Reference:
Get number of arguments passed to function
var x = new Function('a','console.log(arguments)');
x('ff');
Reference
Dynamic argument with parameter
var x = new Function(['a','b','c'],'console.log(arguments); console.log(a+"==>"+b+"==>"+c);');
x('hello','byee','code');
Reference
Passing parameter to the function
var x = new Function(['a','b','c'],'console.log(arguments); console.log(a+"==>"+b+"==>"+c);');
x.apply(null,[12,23,45]);
var x = new Function(['a=12','b=34','c=56'],'console.log(arguments); console.log(a+"==>"+b+"==>"+c);');
x();
Reference:
Execute expression
function executeExpression(expression){
var fun = new Function('value','return ('+expression+')');
return fun(12);
}
executeExpression('12');
Vuejs
expression parser
new Vue({
el:'#vue',
data:{
greeting:'Hello',
name:'Vue',
string:'{{greeting+1}} {{name}} {{name}}! {{1 + 1}}'
},
methods:{
evalInContext(string){
try{
return eval('this.'+string)
} catch(error) {
try {
return eval(string)
} catch(errorWithoutThis) {
console.warn('Error en script: ' + string, errorWithoutThis)
return null
}
}
},
parse(string){
return string.replace(/{{.*?}}/g, match => {
var expression = match.slice(2, -2)
return this.evalInContext(expression)
})
}
}
})
//==>> HTML
<div id="vue">
<div>
{{parse(string)}}
</div>
</div>
Reference: