Frozen global object

If your app uses global object as a config, you should make sure, that this object is truly readonly. So that an attacker can not modify it in dev console. This is how to do it. // example of frozen global object // freeze the config object properties const config = Object.freeze({ httpApi: ‘http://127.0.0.1:20003/v1’, wsApi: ‘ws://127.0.0.1:3000’, enableReduxDevTools: true, minLegalAge: 18 // in years }); // make sure that the config object can not be replaced Object.defineProperty(window, “config”, { value: config, configurable: false, writable: false }); PS: I assume that “window” is sufficient as a global object. If your app runs …

Continue Reading

ES6 generators and async/await

Motivation for this blogpost was my initial ignorance regarding the upcoming ES6 and the generator function. Around christmas 2015 I was looking for a new job, and during one of the assigned test-jobs I had to use generator functions, the .co library, two different databases (one SQL and the other NoSQL)  and node.js. I failed, it was too much of new things to learn at once. But it planted the idea to really learn it and prove that I’ve mastered it. Hence this blog post 🙂 But there are some many new things coming to javascript land, that I still feel …

Continue Reading

Javascript math. LOL

How much is 77.9 – 70? Usually it is 7.9. Right? But not so in Javascript. In Javascript it is 7.900000000000006. Funny as hell 🙂 You can “fix” this by calling .toFixed() on the result number, but it does not fix the cause. As far as I know you can’t fix it, it is part of the language. Read more on StackOverflow Just for case that your browser doesn’t support the .toFixed() method here it is. if (!Number.prototype.toFixed) { Number.prototype.toFixed = function(value, precision) { ///<summary>Returns fixed number of decimal values</summary> var power = Math.pow(10, precision || 0); return String(Math.round(value * power) …

Continue Reading

Convert local time to UTC time in Javascript

This might come in handy. Simple function which converts local time to UTC (Universal Coordinated Time). It accepts single argument of type Date. //converts local time to UTC (Universal Time) function toUTC(/*Date*/date) { return Date.UTC( date.getFullYear() , date.getMonth() , date.getDate() , date.getHours() , date.getMinutes() , date.getSeconds() , date.getMilliseconds() ); } //toUTC()

Continue Reading