Thursday, July 7, 2016

String primitive - JavaScript study

July 7, 2016

Motivation talk:
Given an array, for example, ['a','b','c','e'], insert 'd' before 'e' in the array. Thinking in JavaScript.
1. Do not know JavaScript splice API, write a loop;
2. Know JavaScript splice API,
    var arr = ['a','b','c','e'];
    arr.splice(3,0,'d');
3. Know JavaScript splice API, but careless counting error, 3 goes to 2
    var arr = ['a','b','c','e'];
    arr.splice(2,0,'d');
   Assuming that the array is longer than length 3, and also need to remove some elements before insertion. Counting error is a reasonable mistake.
    Consequence:
    Take extra 5 - 10 minutes to find the issue.

4. Ideal solution:
   Use indexOf to find 'e' position, and then, call splice next.
    var arr = ['a','b','c','e'];
    arr.splice(arr.indexOf('e'),0,'d');

So, the programmer can be lazy, no counting, just ask indexOf API's help, and then, ask splice API's help. Get smart, invest time to get familiar with all APIs, then, it is the game time.

Start to work on JavaScript string reference:
Target: 
1. Memorize all APIs;
2. Write down learning process - notes, blogs to read, questions to ask myself, measurement of progress etc.


JavaScript String reference (30 minutes a time to study -Aug.2, 2016, )
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

Properties:
string.prototype
string.length

Methods:

Exclude deprecated methods, try to remember all the following methods: 34 methods

String.fromCharCode()
String.fromCodePoint()


String.prototype.anchor()
String.prototype.charAt()
String.prototype.codePointAt()
String.prototype.concat()
String.prototype.endsWith()


String.prototype.includes()
String.prototype.indexOf()
String.prototype.lastIndexOf()
String.prototype.link()
String.prototype.localeCompare()


String.prototype.match()
String.prototype.normalize()
String.prototype.repeat()
String.prototype.replace()
String.prototype.search()


String.prototype.slice()
String.prototype.split()
String.prototype.startsWith()
String.prototype.normalize()
String.prototype.substr()

String.prototype.substring()
String.prototype.toLocaleLowerCase()
String.prototype.toLocaleUpperCase()
String.prototype.toLowerCase()
String.prototype.toString()

String.prototype.toUpperCase()
String.prototype.trim()
String.prototype.trimLeft()
String.prototype.trimRight()
String.prototype.valueOf()


String.prototype[@@iterator]()
String.raw()


July 7, 2016 - More than 60 minutes reading - get into the detail - "wild wild west", JavaScript coding is such a challenging one. 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

substring
data descriptor/ accessor descriptor

In order to understand string - JavaScript, go through a lot of topics:
1. defineProperty
2. template literals
...

10 minutes reading of define property
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

string literal
global object directly
Template literals

back-tick (` `)
Expresssion interpolation
syntactic sugar making substitutions like this more readable

tagged template literal - more advanced form of template literals

10 - 15 minutes reading of template literals

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

August 2, 2016  30 minutes+ 
// August 2, 2016 - read JavaScript 0 Standard built-in object > string - 30 minutes reading 

1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined


2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt
read polyfill - read the defintio of the function is fun:

charCodeAt()
defineProperty

Number()
TypeError()

3. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat

4. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith

5. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes

6. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf

syntax: str.lastIndexOf(searchValue[, fromIndex])
optional argument: fromIndex

It can be any integer. Default value is +Infinity. If fromIndex >=str.length, the whole string is searched. If fromIndex < 0, the behavior will be the same as if it would be 0. 

No comments:

Post a Comment