项目作者: suprajaarthi

项目描述 :
Basics of Javascript
高级语言: JavaScript
项目地址: git://github.com/suprajaarthi/JS-Basics.git
创建时间: 2020-12-31T15:50:47Z
项目社区:https://github.com/suprajaarthi/JS-Basics

开源协议:

下载


JS-Basics

ARROW FUNCTION

  1. //regular function call
  2. function add(a)
  3. {
  4. a="supraja";
  5. return a+"arthi";
  6. }
  7. // Remove function kewyord
  8. add = (a) =>
  9. {
  10. a="supraja";
  11. return a+"arthi";
  12. }
  13. // Remove {} curly braces and parse parameter within () paranthesis
  14. add=(a="supraja") => a+"arthi";

EVENT HANDLER

  • const variable = document.querySelector(“.classname”);

  • window.getComputedStyle(variable);

  • It gives the color, font family and everything in the tag / class

OBJECTS

  1. var user = {
  2. name:"",
  3. getUserName:function()
  4. console.log(`User name is ${this.name}`);
  5. }
  6. // Create obj using "create" keyword
  7. var cat = Object.create(user);
  8. cat.name="cat";
  9. // Create obj using "new" keyword
  10. var bat = new user("bat");
  • obj {prop:value}
  • obj has
    • constructor
    • prototype
  • Assume the following scenario :
  • every time the user signs into a page
  • he is assigned certain properties
  • props like fname,lname,..
  • note that username should be unique
  • each time a new instance of object is created

NEW KEYWORD

  • new kw makes use of the entire syntax
  • It invokes the custom constructor and creating a unique instance every single time
  • new creates a new copy of the entire obj
  • It creates unique instances and objects through the obj call
    • We saw that whenever there is a regular func call it refers to a window obj ,
    • w/o using new keyword it gives undefined
    • bcoz w/o “new” it refers to window obj which is {} empty obj so it gives undefined
    • seperate instances of users are created

THIS KEYWORD

  • this keyword either contains window object / whatever the global obj is
    • in node global obj is {} empty obj
    • or the object we have predefined
      • All regular function calls are window objects
      • If it is an obj call rather than func call, this kw points to the object

PROTOTYPES

Prototyes can inject your own stuff by

  • access or give properties
  • give functions
  • getters
  • setters
  • methods
  • can act as constructors to override the properties
    • It runs outside of the object
    • And accesses the properties of the function
  • Prototype has its properties like
    • fill,filter
    • hasOwnProperty

SELF EXECUTING ANONYMOUS FUNCTIONS

  • Does not have a name
  • Cannot be called
  • Syntax
    1. (function(){
    2. console.log("Say Hello");
    3. })();

FUNCTIONAL PROGRAMMING

  • Instead of being declared in global variable , values are passed in parameters
  • Instead of reusing the same variables, new variables are being used
  • Functions can be used in parameters and return values

LEXICAL SCOPING

  1. function init() {
  2. var firstName = "SuprajCat";
  3. function sayfirstName(argument) {
  4. console.log(firstName);
  5. }
  6. sayfirstName();
  7. }
  8. init();
  9. }
  • init() is called in top of the global executional context
  • sayFirstName() on top of init executional context, another context is going to mount up
  • init() gets executed 1st , then nested funcs get executed one by one

CLOSURE

  • Here init just returns the sayfirstName func
  • So the init context is not cleared , it gets stacked
  • So assigning it to a variable and calling it gives the o/p
  • The parameter can be passed as follows (x:=>any)(y:=>any) due to closure
    1. console.log(addition(7)(7));