JavaScript (Identity Operators, Truthy and Falsy, Switch, For..in, Inheritance by Prototype)

Hyun Seo (Lucy) Lee 이현서·2020년 10월 3일
0

Web Development

목록 보기
2/2
post-thumbnail

10/03/2020

Identity Operators

  • == --> true when they have the same value, so 2 == '2' also returns true.
  • === --> only true when they have the same value AND are a same data type, so 2 === '2' returns false. only 2 === 2 returns true.

--> same logic applies to != and !== .

Truthy and Falsy

  • ex:
    if(name) {
    ....
    }
    --> if name is not null/undefined, then name is considered true.

When coding, be mindful of DRY (Don't Repeat Yourself).

  • Through refactoring, aka improving your code without changing external behavior

(Side note: use toLowerCase() function for comparing strings where capitalization should not matter. List of JS built-in functions: http://hepunx.rl.ac.uk/~adye/jsspec11/builtin.htm)

When using the argument variable inside the function definition, you do not need to use var because it is already considered a local variable when it was defined as the function's argument.

Switch statement

  • if you don't include break; after a case that matched, all the cases that are below it and their commands will be run no matter what, as well.
  • usually, you put default at the end of the switch statement, as it is what gets run if none of the cases match the variable's value. However, if you insist to put the default at the start of the switch statement, you must include break; at the end of the default block.

For...in

  • Can iterate through items in an Object, instead of using index.
    ex: for (var name in nameObject) {
    console.log( name + ": " + nameObject[ name ] );
    }
  • when you do var name in nameObject like the above case, the variable name will contain the value of keys in the nameObject. You must do nameObject[name] in order to access the actual values that correspond to the keys.
  • you can use for..in for arrays but it won't guarantee an orderly execution like using indices will. for...in will also access built-in methods and properties that are not of importance for the given program, as basically all things in JS are Objects and have those built-in stuff.

Inheritance through Prototype

  • Whenever we try to access an Object's property, JS checks if that property is one that we defined. If not, JS uses the property of prototype (which is inside that Object's constructor.)
  • For ex, all strings in JS inherit the prototype properties and methods from the String constructor. If you type String.prototype on the console, you'll see all the built-in properties of String Object.
  • It is important to note that whenever you add a property or a method to an Object.prototype, the property/method gets added globally, meaning it will affect all the other codes as well. Just always better not to edit the prototype's properties/methods.
  • Though it should never happen, if the above does happen, you can protect your code from the modified prototype properties/methods by doing myObject.hasOwnProperty( "property_name" ) so that you'll only receive true if the property name is one that you personally assigned when creating myObject. hasOwnProperty() function will return false if it's an inherited property from the prototype.

0개의 댓글