Skip to main content

JavaScript Conference: How function is called?

Javascript is running from washing machine to robot. It is no longer a scripting language. JS is created in 1994.
# Javascript types
  1. Functional (closure)
  2. Object-oriented (class, object, polymorphism, inheritance, abstraction)
# Language types: Interpreted language and compile language
# Function can be called in 4 ways:

1. Function Invocation Pattern
Example:
function add(num1, num2){
      console.log(this);
}
var res = add (8, 9);
console.log(res);
Output: Global object

2. Call function as new operator i.e Construction Invocation Pattern
Example:
function add(num1, num2){
      this.foo = num1;
      console.log(this);
}
var res = new add (8, 9);
console.log(res);
Output: add {foo: 8}
add {foo: 8}

3. Call function as an object (Method Invocation Pattern) eg. abc.name
4. Call function indirectly using method (Indirect Invocation Pattern)
Example:
function add(num1, num2){
      console.log(this);
}
var Cat{
     name: 'foo'
}
var res = add.apply(cat, 8, 9);
console.log(res);
Output: {name: 'foo'}
undefined

JS Example:
var add = function(num1, num2){
     return num1 + num2;
}
Output: A function doesn't depend on how you have created it, it depends on how you are calling that function. so js will determine what this function will be going to return. Always find out where the function is being called, then find out how you are calling that function. If there is nested function, it will be complex to work on arguments.
Demo: 
function add(num1, num2){
       return num1 + num2;
}
1. var res = add(8, 9)


    console.log(res) //17  //Function Invocation Pattern

2. var res = new add(8, 9) //add {} (object)
3. var foo = {}
var res = foo.add(8, 9) //method
4. var foo = {}
     var res = foo.add.apply(this, 8, 9); //Indirect

#this function (depends on how you're calling the function.)
1. Calling function as function (global object)
2. Calling function as construction (newly created object)
3. Calling function using method(whatever the object before dot that will be the value of this)
4. Calling function using indirect (whatever the value you're passing here, that will be the value this inside the function.)

Note: this 4 pattern will control the value of this and return type. A return statement will be going to behave exactly the same in FIP, MIP, IIP and only in construction pattern will ignore the return statement the way we saw demo return you newly created object.
Note: 2015 (major js upgrade) -> array, let, scoping, proxy, object, all they became modules, part of js in es 2015. The 2019 year has a 2018 standard version. Each September, they launch it.
Example 1:
# function add(num1, num2){
      return num1+ num2;
}
var res = add(8);
console.log(res); //Output: Nan (Not a number)

# Example 2: 
res = res + Nan; //Nan //where res= 17
if(res == NaN){
    console.log('hey');
}else{
     console.log('shut up');
}
Output: 
shut up
number (typeOfNaN)

Example 3:
if(NaN === NaN){
     console.log('hey');
}else{
     console.log('shut up');
}
Output: Shut up
number

# Example 4:
var car = {
       name: 'foo'
}
console.log(cat.name) //foo
cat.name = 'koo';
console.log(cat.name) //koo

# Example 5:
var cat = {
        name: 'foo',
         age: 9
}
console.log(Object.getOwnPropertyDescriptor(Cat, 'name'));
Output:
{
       value: 'foo',
       wrtiable: true,
       enumerable: true,
       configuration: true
} // object property descriptor

# Example 6:
var cat = {
    name: 'foo'
}
Object.defineProperty(Cat, 'name', {
        writable: false
})
console.log(cat.name) //foo
cat.name = 'koo';
console.log(cat.name) //foo (because writable is false)

Source: JS Conference

Comments

Popular posts from this blog

Javascript: Object to Array and Array to Object

Output: Final Array [ { mango: 1 }, { apple: 2 }, { banana: 3 }, { orange: 1 }, { watermelon: 1 }, { papaya: 1 } ] Output: Final Object { mango: 1, apple: 2, banana: 3, orange: 1, watermelon: 1, papaya: 1 }

Javascript: Callback and High Order Function

CALLBACK A callback is a function that gets invoked after an asynchronous result appears. a callback function is an async function argument. - Asynchronous code result handle using callback and promises. // callback  function function with argument  // ---argument :// if a function pass-through function as an argument, it is higher-order function. function needs to be called in order to execute. a callback is used when calling a function asynchronous  What is a Callback or Higher-order Function? A callback function , also known as a higher-order function, is a function that is passed to another function (let’s call this other function “otherFunction”) as a parameter, and the callback function is called (or executed) inside the otherFunction. A callback function is essentially a pattern (an established solution to a common problem), and therefore, the use of a callback function is also known as a callback pattern. High-order function is a function that takes ...

Javascript: Frontend - AngularJS

# FrontEnd Technologies: # Web (Internet) - Html, css framework (bootstrap, material design-> component based) Angular 2 - Single page application (fast). - Web application or website that interacts with the user by dynamically rewritting the current page rather than loading entire new pages from a server. - Can do asynchronous jobs. # Tools: - Typescript - VS code # MVVW (Model View ViewModel Pattern) -> two-way data binding - In angular, the controller (the C in MVC) is replaced by ViewModel (the VM in MVVM). # Typescript Programming Language Typescript (.ts) is imp in angular 2. - supersetup js - strictly typed program - can do class based oop - future programming language. - security maintain (TS code compile and run in js) - JS with additional features. # Data Binding - Controller and Views synchronize. Types: 1. Event -  eg: click, onClick, change 2. Property - eg: hide, show 3. 2-way binding - Reflect in view if changes in model- c...