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

NodeJS: Request and Respose Objects

# req Objects - req - req.body, req.queries, req.validationerrors - res.headers, req.users, req.file, req.assert - req.params, req.checkbody #res Objects - res.send, res.end, res.json, res.render, res.status, res.sendStatus # - .limit(4) //limit only upto 4 docs - skips(3) //skip first 3 docs - exe //query build and then execute, it is also callback function of mongoose. - sort({_id: -1}) //for decending order - populate('userId') //populating data inside the reference.

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 }

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