Lingquan | Blog

Small but important stuff about Javascript

Word count: 584Reading time: 3 min
2018/10/20 Share

if (key in object) VS if (object.hasOwnProperty(key))

  • in will also return true if key gets found somewhere in the prototype chain,
  • whereas Object.hasOwnProperty (like the name already tells us), will only return true if key is available on that object directly (its “owns” the property).

    !!(expression) in js

  • it convert expression into boolean

    Ways to judge if two object are same

  • _.isEqual(obj1, obj2) method of lodash.js and underscore.js
    from here

    undefined and null

  • The undefined value behaves as false when used in a boolean context;The undefined value converts to NaN when used in numeric context.
  • When you evaluate a null variable, the null value behaves as 0 in numeric contexts and as false in boolean contexts
  • variables that are hoisted return a value of undefined. So even if you declare and initialize after you use or refer to this variable, it still returns undefined

    function scope vs block scope

  • block scope is delimited by a pair of curly brackets
  • function scope is delimited by function
  • In web pages, the global object is window

    falsy values

    The following values evaluate to false (also known as Falsy values):

  • false

  • undefined
  • null
  • 0
  • NaN
  • the empty string (“”)

All other values, including all objects, evaluate to true when passed to a conditional statement.

  • Do not confuse the primitive boolean values true and false with the true and false values of the Boolean object. For example:
    var b = new Boolean(false);
    if (b) // this condition evaluates to true
    if (b == true) // this condition evaluates to false

Data type conversion

  • In expressions involving numeric and string values with the + operator, JavaScript converts numeric values to strings
  • n statements involving other operators, JavaScript does not convert numeric values to strings.
  • convert strings to numbers: parseInt() and parseFloat()

    Object Literals

  • Object property names can be any string, including the empty string. If the property name would not be a valid JavaScript identifier or number, it must be enclosed in quotes. Property names that are not valid identifiers also cannot be accessed as a dot (.) property, but can be accessed and set with the array-like notation(“[]”).
  • identifier are a sequence of characters
    var foo = {a: 'alpha', 2: 'two'};
    console.log(foo.a); // alpha
    console.log(foo[2]); // two
    //console.log(foo.2); // SyntaxError: missing ) after argument list
    //console.log(foo[a]); // ReferenceError: a is not defined
    console.log(foo['a']); // alpha
    console.log(foo['2']); // two

arguments objects

  • it is a array-like object
  • Using the arguments object, you can call a function with more arguments than it is formally declared to accept. This is often useful if you don’t know in advance how many arguments will be passed to the function.
    function myConcat(separator) {
    var result = ''; // initialize list
    var i;
    // iterate through arguments
    for (i = 1; i < arguments.length; i++) {
    result += arguments[i] + separator;
    return result;
    // You can pass any number of arguments to this function, and it concatenates each argument into a string "list":

    // returns "red, orange, blue, "
    myConcat(', ', 'red', 'orange', 'blue');

    // returns "elephant; giraffe; lion; cheetah; "
    myConcat('; ', 'elephant', 'giraffe', 'lion', 'cheetah');

From MDN

Arrow function

  • does not have its own this, arguments, super
  • An arrow function does not have its own this; the this value of the enclosing execution context is used(箭头函数捕捉闭包上下文的this值).
  • 在箭头函数出现之前,每一个新函数都重新定义了自己的 this 值
  1. 1. if (key in object) VS if (object.hasOwnProperty(key))
  2. 2. !!(expression) in js
  3. 3. Ways to judge if two object are same
  4. 4. undefined and null
  5. 5. function scope vs block scope
  6. 6. falsy values
  7. 7. Data type conversion
  8. 8. Object Literals
  9. 9. arguments objects
  10. 10. Arrow function