Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Evolution of Function, function statement vs Expression
- //first class function
- //javascript "this"
- //function statement
- // function sum(num1, num2){
- // return num1 + num2
- // }
- //function expression
- // const sum = function(num1, num2){
- // return num1 + num2
- // }
- // //arrow function
- // const sum = (num1, num2) => {
- // return num1 + num2
- // }
- // const sum = (num1, num2) => num1 + num2
- // const sum = num1 => num1 + num1
- // console.log(sum(1, 2))
- //statement vs Expression
- //statement means command
- //Expression means value
- //can be written as right side of variable (expression)
- //can't be written as right side of variable (statement)
- //first class function
- //criteria
- //can be written right side of a variable
- //can be passed as arguments, receive as a parameter
- //can be return from function
- //Higher order Function function, callback
- //A higher order function is a function that takes a function as an argument, or returns a function
- // function higherOrderFunc(fn){
- // fn(1, 2)
- // }
- // const callbackFn = function (num1, num2) {
- // console.log(num1 + num2)
- // }
- // higherOrderFunc(callbackFn)
- //Higher order function
- // function sum(num1){
- // return (num2) => {
- // return num1 + num2
- // }
- // }
- // const innerFn = sum(3)
- // console.log(innerFn(4))
- // console.log(sum(3)(4))
- //Javascript "this"
- //this outside of function(window)
- //this inside of a function
- //depends on how you call the function
- //if it is called in a plain way (window) strict mode(undefined)
- // if you call as object reference it will indicate the object
- //If you call with constructor function "this" indicates empty object and can be fulfilled and returned
- //outside of function
- // console.log(this)
- // //inside of function
- // function thisFn() {
- // console.log(this)
- // }
- // thisFn()
- //attaching with window object
- // var firstName = 'Hono'
- // var lastName = 'Mono'
- const profile = {
- firstName: 'samim',
- lastName: 'Hasan',
- fullName() {
- const $this = this
- function innerFunc() {
- console.log('innerFunc', $this)
- }
- innerFunc() //plain way
- // innerFunc.call(profile) //plain way
- return this.firstName + ' ' + this.lastName
- }
- }
- console.log(profile.fullName)
- const fullNameRef = profile.fullName
- console.log(fullNameRef.call(profile))
- // console.log(profile.fullName())
- // function fullName(age1, age2) {
- // return this.firstName + this.lastName + age1 + age2
- // }
- // const obj = { firstName: 'samim', lastName: 'Hasan' }
- // const result = fullName.call(obj, 27)
- // const result = fullName.apply(obj, [27, 30])
- //like call (but do not call the function)
- //for call and apply
- // console.log(result)
- // const result = fullName.bind(obj, 30, 35)
- // //for bind
- // console.log(result())
- //constructor function
- // function Profile() {
- // // console.log(this)
- // this.firstName = 'Honu'
- // this.lastName = 'Monu'
- // this.fullName = function () {
- // return this.firstName + this.lastName
- // }
- // }
- // const profile = new Profile()
- // console.log(profile)
- // console.log(profile.fullName())
- let callArray = [0, null, '', undefined, 2, 3]
- function truthyValue(findtruthy) {
- let arr = []
- if (Array.isArray(findtruthy)) {
- for (let elm of findtruthy) {
- if (elm) {
- arr.push(elm)
- }
- }
- } else {
- console.log('Not array')
- }
- return arr
- }
- const checkIngArray = truthyValue(callArray)
- console.log(checkIngArray)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement