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 n...

Github Tutorial

GitHub brings together the world's largest community of developers to discover, share, and build better software. Delivered through software as a service(SAAS) business model 2008 by linus (Owner: Microsoft)  #Create New Repository in Github New Repository Add Repository Name and description Public / Private, initialize this repo with Read Me Create repository create a new repository on the command line echo "#test" >> README.md git init git add README.md git commit -m "first commit" git remote add origin https://github.com/sinuna/test.git git push -u origin master …or push an existing repository from the command line git remote add origin https://github.com/sinuna/firstRepository.git git push -u origin master …or import code from another repository You can initialize this repository with code from a Subversion, Mercurial, or TFS project. Initialize Repo $ git init Clone Repo $ git clone https://github.com...

Wordpress React Theme Development

1) Download wordpress file from https://wordpress.org/download/ 2) Open xampp, wamp or openserver. I will show with xampp server. Also Xampp server recommended too. 3) Add wordpress folder to htdocs folder (for xampp) 4) cd xampp\htdocs\wordpress\wp-content\themes 5) npx create-react-wptheme react_theme 6) cd react_theme/react-src 7) yarn start 8) go to wp-admin dashboard change worpdress theme to react_theme 9) again yarn start 10) yarn build App.js http://localhost/wordpress_react/?rest_route=/wp/v2/posts import React , { Component } from 'react' ; class App extends Component { constructor ( props ){ super ( props ); this . state = { data : [] } } componentDidMount () { return fetch ( `http://localhost/wordpress_react/?rest_route=/wp/v2/posts` ) . then (( response ) => response . json ()) . then (( responseJson ) => { this . setState ({ data : ...