📘
snesjhon
  • You Should Know
  • Personal
    • Blogs
      • I'll never complain about web tooling again.
      • vscodevim
  • JavaScript
    • Closures
      • Closure Q1
    • Values
      • Primitive vs Reference
      • Accessing by value and reference
    • Function
      • call, apply, bind
      • Pass by value
      • Different types of Scopes
      • Context vs Scope
      • Parse Time Vs Run Time
    • Hosting
      • Are Let & Const Hoisted?
      • Hoisting Q1
      • Hoisting Q2
    • Standard Objects
      • Math
        • Math.log10
        • Math.pow()
    • Array
      • Apply
      • Slice vs Splice vs Split
    • This
      • This - Q1
    • TypeScript
      • as const
    • FAQs
      • Modulo Operator
      • Timeout
      • Declarative vs Imperative
      • ++i vs. i++
    • Interview Questions
  • react
    • FAQs
  • Ruby
    • Debugging
    • Symbols
    • Intro
  • Algorithms
    • Sliding Window
      • minSubArrayLen
      • maxSubArraySum
      • findLongestSubstring
    • Frequency Counter
      • sameFrequency
    • Recursion
      • nestedEvenSum
      • flatten
      • Reverse a String
      • Fibonnacci
    • Searching
      • overlappingIntervals
      • twoStringSearch
      • binarySearch
    • Sort - Elementary
      • Selection Sort
      • Bubble Sort
      • Insertion Sort
      • quickSort
    • Sort - Intermediate
      • Radix Sort
      • Merge Sort
    • FAQs
  • Data Structures
    • Breadth First Search
    • Linked Lists
      • Singly Linked Lists
    • FAQs
  • Code Challenges
    • LeetCode
      • removeDuplicates
    • Hacker Rank
      • twoSums
      • Sock Merchant
    • Hacker Rank - Medium
      • New Year Chaos
  • Databases
    • SQL
Powered by GitBook
On this page
  • Call
  • Questions
  1. JavaScript
  2. Function

call, apply, bind

Call

function a(){
    console.log('a');
}

// These two are the same
a() === a.call()

Questions

var b = {
    name: 'jay'
    say(): {
        console.log(this)
    }
}

var c = {
    name: 'jay'
    say(): {
        return function() {
            console.log(this)
        }
    }
}

var d = {
    name: 'jay'
    say(): {
        return () => console.log(this)
    }
}

// What would each return?
b.say() 

c.say()()

d.say()()
var b = {
    name: 'jay'
    say(): {
        console.log(this)
    }
}
b.say()

b.say() would return {name: "jay", say: fn()} Because the say() function is "bounded" to the b object

var c = {
    name: 'jay'
    say(): {
        return function() {
            console.log(this)
        }
    }
}
c.say()()

This would return window that's because the function isn't bounded to anything. As in it's . is not called by anything.

c.say() is run, and it'd equal something like var temp = c.say() and then temp()would then be executed. And since temp isn't bounded to anything it'd just be the window object.

var d = {
    name: 'jay'
    say(): {
        return () => console.log(this)
    }
}
d.say()()

This would return {name: 'jay', say: fn} Although it might look the same as c the main difference is that arrow function is lexically scopes the function

Last updated 4 years ago