Skip to content

Latest commit

 

History

History
232 lines (186 loc) · 5.85 KB

2.5-New utility methods for Strings, Arrays and additions to the Number and Object object.md

File metadata and controls

232 lines (186 loc) · 5.85 KB

2.5-New utility methods for Strings, Arrays and additions to the Number and Object object

String.prototype and Array.prototype

Array.prototype.fill()

console.log( ['a', 'b', 'c'].fill("") ); // Array [ "", "", "" ]

Ej1: fill

Array.prototype.find()

console.log( [1, 2, 3].find(x => x == 3) );

Ej1: find

Array.prototype..findIndex()

[6, 8, -5].findIndex(x => x < 0)

Ej1: findIndex

String.prototype.startsWith()

var str = 'Hello World!';
console.log(str.startsWith('Hello')); // True

Ej1: startsWith

String.prototype.endsWith()

var str = 'Hello World!';
console.log(str.endsWith('Hello')); // False

Ej1: endsWith()

String.prototype.includes()

var str = 'Hello World!';
console.log(str.includes(' ')); // True
console.log( 'JS '.repeat(3) ); // JS JS JS

Ej1: includes()

Array.of(element0[, element1[, ...[, elementN]]])

var arr = new Array([1,2,3]); // ES4.1
var arr1 = [1, 2, 3]; // ES4.1
let arr2 = Array.of(1, 2, 3); // ES6

console.log(arr); // 1,2,3
console.log(arr1); // 1,2,3
console.log(arr2); // 1,2,3 

Ej1: Array.of

Array.from(arrayLike[, mapFn[, thisArg]])

function a(){
  return Array.from(arguments);
}
console.log( a(1, 2, 3));  // [1, 2, 3]

eg: Array.from

Additions to the Number Object: isNaN

Number.isNaN(0 / 0);   // true
Number.isNaN(NaN);        // true
Number.isNaN(undefined);  // false
Number.isNaN({});         // false

Additions to the Number Object: isFinite

Number.isFinite(NaN);       // false
Number.isFinite(0);  

Additions to the Object object: Object.is()

== and === both Operators are comparing same things, but == allows coercion and === disallows coercion, making === faster and more accurate.

Following best practices: *Avoid == potential coerce errors *Use === operator and also make it faster or Object.is()

console.log(1 == "1"); // true
console.log(1 === "1"); // false
Object.is(1,"1"); // false

console.log(NaN == NaN);
console.log(NaN === NaN); // false
Object.is(NaN,NaN); // true

console.log('' == 0);  // true
console.log('' === 0);  // false
console.log(Object.is('',0); // false
// doesn’t work on IE11

Additions to the Object object: Object.assign()

For merging objects we will have the function

var obj = { a: 1 };
var copy = Object.assign({}, obj); // Object.assign(target, source_1, ..., source_n)
console.log(copy); // { a: 1 }

Computed property keys

let o1 = 'Jon';
let o2 = {[o1]: false,
          ['D'+'o'+'e']: null
};
console.log(JSON.stringify(o2));

Ej1: Computed property keys

template literal

let person = { name: 'Mr.' };
let name = 'Leo';
let tpl = `I'm: ${person.name} ${name}`; // we are using  with `back-ticks` or `grave-accent`
console.log(tpl);

Ej1: template literal

In addition to the classic for-in statement, ES6 has added the for-of statement .

Strings are iterable, which means that you can use for-of to iterate over their characters While for-in iterates over "property names", for-of iterates over "property values"

basic syntax for-in

for (let index in 'Jon Doe') { // property names
    console.log(index); // 0,1,2,3,4,5,6
}

Eg: for-in

for (let index of 'Jon Doe') { // property values
    console.log(index); // J,o,n,,D,o,e
}

Eg: for-in

var arr = [1, 2, 3];
for (var n of arr) console.log(n); // 1,2,3

Eg3: simple for-of loop throw Array

for (let index of 'Jon Doe') {
    console.log(index);
}

Eg1: for-of

let arr = ['Hello', 'world', "!"];

for (let element of arr) {
  console.log(element);
}

for (let [index,element] of arr.entries()) {
  console.log(`${index}. ${element}`);
}

Eg2: for-of with template literal

Symbol()

A new primitive type: Symbol() They are tokens that serve as unique IDs. This key avoid accidentally clash with any other property key:

let symbol1 = Symbol();
let symbol2 = Symbol();

console.log(typeof symbol1);
console.log( Object.is(symbol1,symbol2) );

let wrap = Object(symbol2);
console.log(typeof wrap);
console.log(wrap instanceof Symbol)
console.log('symbol2 symbol: ' + String(symbol2) );

Ej1: Symbol

computed property keys

You can specify the key of a property via an expression, by putting it in square brackets. In the following object literal, we use a computed property key to make the value of MY_KEY the key of a property.

const _id = Symbol(); // generate Unique token and cannot be changed
let obj = {
    [_id]() { // specify the key of a property via an expression
        return 'ID007';
    }
};
console.log( obj[_id]() ); // ID007

Ej1: computed property keys

Default Parameter Values

var foo1 = function(n) {
    n = n || "Leo";
    console.log( n )
};

let foo2 = function(n = "Leo") {
    console.log( n );
};

foo1(); // Leo
foo2(); // Leo

Ej1: Default Parameter ES5 Vs ES6

⬆ back to top