- objects pass by reference not copy
- when any enclosed property is deleted, it'll delete the current object's property however due to object reflection, it'll now inherit parent objects valye for that property.
- object reflection: every object intrinsically spans its control back to the Object.prototype spec, even those objects literals we define internally map to that.
- anoymous functions: self-invokating functions with no name
- method: when
this
applies to its own definition like having a function defined as an property to an object, making its scope relevant within the parent object:
const obj = {
val: 2612,
getVal: function() {
return this.val;
}
};
console.log(obj.getVal()): // 2612
- function: when object does not have a property that corresponds to that function, it binds to the global scope instead
const add = function(a, b) => {
return a + b;
};
const sum = add (2600, 12); // returns 2612
- constructor: any class-based (or that augments behavior of a generic OOP class) functions is bound to that class's scope
class _ {
this.value = null;
constructor() {}
constructor(val) {
this.value = val;
}
getValue() {
return this.value;
}
};
let __ = new _();
__.getValue(); // returns null
__ = new _(2612);
__.getValue(); // returns 2612
- apply: takes an array of argument to invoke the function
const add = function(a, b) => {
return a + b;
};
const sum = add (null, [2600, 12]); // returns 2612
let something = "";
const _2612 = [2, 6, 1, 2];
const print = function(i) {
if (i >= _2612.length)
return something;
something += _2612[i];
return print(i+1);
};
print(0); // returns "2612"
- cascading: its a very generic concept to chain many functions togther to accomplish some task, here it goes:
// making a synchronous call that returns a Promise
const flag = false; // if payload was returned by server
api.getSomething(<BODY-CONTENTS>)
.then(result => if (result) flag = !flag)
.catch(error => throw new Error(error))
.finally(console.log("this will run at the very last, after then and catch execute \(or not\)"))
- custom function to check if an object is an object OR array:
const obj = {firstName: "Sambhav", lastName: "Jain"};
const arr = [2, 6, 1, 2];
var is_array = function (value) {
return value &&
typeof value === 'object' &&
typeof value.length === 'number' &&
typeof value.splice === 'function' &&
!(value.propertyIsEnumerable('length'));
};
console.log(is_array(obj)); // return false
console.log(is_array(arr)); // return true
- custom function to build a
n*n-length
matrix:
const arr = [];
const matrixBuilder = function(length) {
for (let i = 0; i < length; i++) {
arr.push(new Array(length));
}
return arr;
};
matrixBuilder(3); /* returns [[empty, empty, empty],
[empty, empty, empty],
[empty, empty, empty]] */
- custom function to sort any two objects by a specified key:
function sorter(a, b, key) {
if (typeof a === "object" && typeof b === "object") { // check if object
if (a.hasOwnProperty(key) && b.hasOwnProperty(key)) { // check if both objects have the key
return a[key] > b[key] ? 1 : -1;
} else return 0;
} else return 0;
}
const arr = [{first: "Sambhav", last: "Jain"}, {first: "PseudoSambhav", last: "PsuedoJain"}];
arr = arr.sort((a, b) => sorter(a, b, "x")); // returns the array as it is
arr = arr.sort((a, b) => sorter(a, b, "first")); // returns [{first: "PseudoSambhav", last: "PsuedoJain"}, {first: "Sambhav", last: "Jain"}]
arr = arr.sort((a, b) => sorter(a, b, "last")) // returns [{first: "Sambhav", last: "Jain"}, {first: "PseudoSambhav", last: "PsuedoJain"}]