Javascript is running from washing machine to robot. It is no longer a scripting language. JS is created in 1994.
# Javascript types
# Function can be called in 4 ways:
1. Function Invocation Pattern
2. Call function as new operator i.e Construction Invocation Pattern
3. Call function as an object (Method Invocation Pattern) eg. abc.name
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)
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
# Javascript types
- Functional (closure)
- Object-oriented (class, object, polymorphism, inheritance, abstraction)
# 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
Post a Comment