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

Deploy react app on digitalocean using nginx

Source:  https://www.learnwithjason.dev/blog/deploy-nodejs-ssl-digitalocean/ Source:  https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04 Source:  https://www.youtube.com/watch?v=vRrhQwNixlc https://www.youtube.com/watch?v=pyxXCwgWdMw https://dev.to/xarala221/the-easiest-way-to-deploy-a-react-web-application-2l8a 1. Create a new droplet on DigitalOcean. Choose the $5/month option with Ubuntu 16.04.1 x64. Select a region closest to your users. 2. Finally, add your SSH key and ls -la ~/.ssh # This copies the key so you can paste with command + V pbcopy < ~/.ssh/id_rsa.pub # This prints it in the command line for manual copying cat ~/.ssh/id_rsa.pub   3.  Add your SSH key to the droplet Click “Add SSH Key” to save it, then make sure it’s selected, name your droplet, and hit the big “Create” button to get your server online. Your new droplet will display

Gallery

https://fancyapps.com/fancybox/3/ <!-- 1. Add latest jQuery and fancybox files --> <script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" /> <script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script> $ ( function () { if ( $ ( "[data-fancybox]" ). length ){ $ ( "[data-fancybox]" ). fancybox (); } });

Javascript: Array

ARRAY // homogenious and heterogenious array  var a = 'I am here'; var status = true; //array -> multiple values var arr = new Array(); //constructor method var arr1 = []; //bracket notation Example: var hobbies = ['dancing', true, 3423,{ //multiple type data collection values dance: true, singing: false }, []];