diff options
-rw-r--r-- | scratch.js | 51 |
1 files changed, 50 insertions, 1 deletions
@@ -24,4 +24,53 @@ } */ -console.log("hello, world!") +//console.log(String('hello lmao wtf').split('.')); +const sumArray = (arr) => arr.reduce((prev, curr) => prev + curr); +myArr = [3, 7, 9, 12]; +console.log("the sum of %O array is: %d", myArr, sumArray(myArr)); + +console.log("%s is %d years old", 'Jesus', 33); +console.log("%chello, %cworld%c!", "color: blue;", "font-size: 2em;", "/* no CSS rule /*"); + +// class-like object +function Person(name) { + this.name = name; // public data member + var _age; // private data member + + function privFunc() {} + this.setAge = function(n) { _age = n; } + this.getAge = function() { return _age; } // this.age would be undefined age is 'var'ed' +} +Person.prototype.funkyName = function() { return this.name + 'ley' } // public method to all +var kyle = new Person("Kyle"); +console.log(kyle.funkyName()); + +// convert list to array +function list() { + return Array.prototype.slice.call(arguments) +} +let list1 = list(1, 2, 3) // will get converted to [ 1, 2, 3 ] +console.log(list1); + +// split string into words +var str1 = "this is a string, ayy"; +var words = str1.split(/,| /).map(w => console.log(w)); + +// .format() implement into built-in String object +String.prototype.format = function() { + var formatted = this; + for (var arg in arguments) { // + console.log("arg is %s", arg); + formatted = formatted.replace("{" + arg + "}", arguments[arg]); + } + return formatted; +}; +console.log("{0} is dead, but {1} is alive!".format("ASP", "ASP.NET")); + +// .format implement into built-in String object using ES6 declarative approach +String.prototype.format = function() { + return [...arguments].reduce((p,c) => p.replace(/%s/,c), this); +}; +console.log('Is that a %s or a %s?... No, it\'s %s!'.format('plane', 'bird', 'SOman')); + +console.log((function(){}).constructor); |