summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2020-07-19 07:07:08 -0500
committerKyle K <kylek389@gmail.com>2020-07-19 07:07:08 -0500
commit3b143db708b8fbea5cebccc2b56f4cd1e5d7637a (patch)
treea77cfb579bd6c90737b5fbcee68115c180c97aa3
parent84b0f19552acdcf5e54c39664a3a00f13d299ffd (diff)
downloadjsexamples-3b143db708b8fbea5cebccc2b56f4cd1e5d7637a.tar.gz
jsexamples-3b143db708b8fbea5cebccc2b56f4cd1e5d7637a.tar.bz2
jsexamples-3b143db708b8fbea5cebccc2b56f4cd1e5d7637a.zip
some js examples
-rw-r--r--scratch.js51
1 files changed, 50 insertions, 1 deletions
diff --git a/scratch.js b/scratch.js
index 1f47ce8..f19d931 100644
--- a/scratch.js
+++ b/scratch.js
@@ -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);