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 target object.

So first, we create an object called AnimalClass, with properties x, y to denote coordinates of where this animal is. We then have functions move, which literally assigns integers to the x, y properties. Then, locate, which displays where this animal is on a coordinate.

We then use Object.create to create an empty object called target. The target’s prototype is this AnimalClass. This means that whatever property or function gets appended to target, its prototype is always AnimalClass. This is so that you can inherit AnimalClass’s functionalities on this object.

— target —
{ x: 0, y: 0, locate: [Function: locate], move: [Function: move] }
I’m here: 0, 0

Creating a duck via Object.assign

Now that we have a target object, what we’re trying to do is to copy all the properties and functionalities of source object(s) into this target object. Hence we define a custom anonymous object where we add property “color” and function “speak”.

It returns an object, and we test the inheritance by using its own properties, and its prototype’s functionalities.


— duck —
{ color: ‘black’, speak: [Function: speak] }
quack quack
I’m here: 7, 8


ref – http://shanghaiseagull.com/index.php/2018/02/06/prototype-and-inheritance-in-js-non-class/

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 object can access functionalities from the specified Constructor.

Because instance c is at the highest level of the hierarchy. When it can’t find a functionality, it goes down to Cover Prototype Object. When it can’t find it there, it goes to Image Prototype, and fianlly Object Prototype. Hence, it can touch all Prototype Objects of the constructors. That’s why it is true.

When analyze the cover1 instance shown in the diagram, which of prototype Cover, we see that it can access its functionalities from its own Prototype Cover. It can go up to Image Prototype and Object Prototype. However, it ends there. It cannot go backwards and access CoverArticle prototype at all. Hence, false.


isPrototypeOf is a function on the Object Prototype that tests whether the parameter can check against the prototype.
So for example, if we have an instance of CoverArticle (like c) it can definitely touch Cover.
If we have instance of Cover (like cover1) obviously it touches Cover.

buddha, an instance of Image, only touches Image and Object. It cannot go backwards and check Cover Prototype Object. Thus, it will be false.

Maps WeakMaps, Sets, WeakSets


Map is a collection of keyed data items, just like an Object. But the main difference is that Map allows keys of any type.

The main methods are:

new Map() – creates the map.
map.set(key, value) – stores the value by the key.
map.get(key) – returns the value by the key, undefined if key doesn’t exist in map.
map.has(key) – returns true if the key exists, false otherwise.
map.delete(key) – removes the value by the key.
map.clear() – clears the map
map.size – returns the current element count.

Map can also use objects as keys.

For instance:

Iteration over Map
For looping over a map, there are 3 methods:

map.keys() – returns an iterable for keys,
map.values() – returns an iterable for values,
map.entries() – returns an iterable for entries [key, value], it’s used by default in for..of.

For instance:


A Set is a collection of values, where each value may occur only once.

Its main methods are:

new Set(iterable) – creates the set, optionally from an array of values (any iterable will do).
set.add(value) – adds a value, returns the set itself.
set.delete(value) – removes the value, returns true if value existed at the moment of the call, otherwise false.
set.has(value) – returns true if the value exists in the set, otherwise false.
set.clear() – removes everything from the set.
set.size – is the elements count.
For example, we have visitors coming, and we’d like to remember everyone. But repeated visits should not lead to duplicates. A visitor must be “counted” only once.

Set is just the right thing for that:

swift to dos

Swift TODO:

Course Goal:
Adding source files, resources, links, and literals
Adding pages to a playground
Using overflow operators and bitwise operators
Using ranges with strings
Creating complex sequences
Chaining higher-order functions
Defining lazy properties
Using failable initializers
Mutating methods
Working with singletons
Nesting function types
Creating error types and recursive enumerations
Extending concrete types
Referencing selectors and key paths
Working with protocol-oriented programming
Defining class-only protocols and optional protocols
Using option sets, type checking, and casting operators
Advance your command of Swift 3 by going beyond the fundamentals. Scott Gardner helps you strengthen your programming skills by providing additional hands-on coding instruction. In this course, Scott covers playgrounds, operators, strings, sequences, collections, reference types, protocols, generics, and access control. Prepare to exercise your skills as a developer as you follow along and explore defining properties, working with higher-order functions, subscripting, nesting, and more.
Course Agenda:
What you should know
How to use the exercise files
1. Playgrounds
Add source files
Add resources, links, and literals
Add pages to a playground
Use iPad playgrounds
2. Operators
Use overflow operators
Understand binary notation
Use bitwise operators
3. Strings and Characters
Index into strings
Use ranges with strings
4. Sequences and Collections
Sequences and collections, part 1
Sequences and collections, part 2
Create complex sequences
Use higher-order functions
Chain higher-order functions
5. Control Flow
Use labels
Use if case and for case
6. Value and Reference Types
Define lazy properties
Property observers and accessors
Use failable initializers
Use type checking and casting operators
Use final
Mutating methods
Work with singletons
Nest types
Nest function types
Create error types
Create recursive enumerations
Extend concrete types
Reference selectors and key paths
7. Protocols
Understand protocols
Understand protocol-oriented programming
Define and adopt protocols
Define class-only protocols
Define optional protocol requirements
Work with protocol types
Use option sets
Use type checking and casting operators
External protocols, part 1
External protocols, part 2
Associated types
8. Generics
Understand generics
Work with generics
Constrain generic types
9. Access Control
Understand access control
Work with access control, part 1
Work with access control, part 2

Create sync method around async operation

async method

sync version