Object.assign

ECMAscript 6 introduced Object.assign() to achieve this natively in Javascript. The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. The Object.assign() method only copies enumerable and own properties from a source object to a […]

Functional Inheritance

ref – http://julien.richard-foy.fr/blog/2011/10/30/functional-inheritance-vs-prototypal-inheritance/ https://medium.com/javascript-scene/3-different-kinds-of-prototypal-inheritance-es6-edition-32d777fa16c9 http://therealmofcode.com/posts/2012/11/functional-inheritance-javascript.html https://stackoverflow.com/questions/2800964/benefits-of-prototypal-inheritance-over-classical Functional Inheritance The properties you attach to the returning object are class public interface. Functions and properties you define inside function, but not as a part of the returning object, are private members accessible because of closure and invisible because they were not returned. 1) In the parent function, […]

this context, change it via call, bind

Non Strict In non-strict mode, outside of functions, this is an empty literal object. global object is called global. The only difference is that the ‘this’ in functions, is tied to global. While, in strict mode, the ‘this’ in functions is undefined and not attached to anything.

..or..

Strict mode In strict mode, […]

Data types, variables for JS

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures Quick Tip: How to Declare Variables in JavaScript Data types The latest ECMAScript standard defines seven data types: Six data types that are primitives Boolean Null Undefined Number String Symbol (new in ECMAScript 6) and Object All types (except objects) define immutable values (values, which are incapable of being changed). For example and unlike […]

jQuery lazy loading

Loading the library

Targeting it…and callbacks

Specifying the images Since your jQuery targets class lazy, we must use lazy for classes on img and div tags. You use data-src to specify the location of the images.

Note that for img tags, when the images load, it will change to data-src will change […]

instanceof

ref – http://shanghaiseagull.com/index.php/2018/02/06/prototype-and-inheritance-in-js-non-class/ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof

So we create an instance of CoverArticle called c. It accesses its own prototype function coverArticleFormat. It goes up the hierarchy one step and goes into Cover prototype where it accesses coverFormat. It goes one more step to Image prototype, and accesses print. Instance of lets you know if your […]

Why do we use a temporary box in prototype inheritance?

https://stackoverflow.com/questions/47843315/why-do-we-use-a-temporary-box-in-prototype-inheritance The function below is one that Derek Banas on youtube on his OO Javascript tutorial uses. You have a function A that you want as a child. You have function B that you want as a parent. You want to connect them using a function called extend.

The implementation is:

but why […]

Object.create

Object.create(param) creates an empty object, with its __proto__ pointing to “param”. That way, the empty object can inherit properties from param. We’ll demo this by first creating a literal object with “person” variable referencing it.

Now, in JS, the base Object class is an object that has a prototype reference that points to an […]

OOP in Javascript (with Node)

ref – http://book.mixu.net/node/ch6.html The new constructor call (e.g. new Foo()): creates a new object, sets the prototype of that object to Foo.prototype and passes that as this to the constructor. The prototype chain lookup mechanism is the essence of prototypal inheritance.

Instantiate the class:

Adding private variables

Avoid assigning variables to prototypes […]

static vs non-static

ref – https://stackoverflow.com/questions/45594196/difference-between-static-function-declaration-and-the-normal-function-declarati That’s the very definition of a static function that you don’t have to instantiate the class to call it. That’s not something JS specific. A static method is callable from the class itself A non-static method is callable from an instance of the class, so you basically have to create an object […]