Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //javascript "this"
- //inside of the function (functional context)
- //outside of function (global context)
- //window obj(global context) -Don't use
- console.log(this)
- ;('use strict')
- function whatIsThis() {
- //In strict mode this indicates undefined otherwise widow object
- console.log(this)
- }
- whatIsThis() //plain function call (window)
- const calculator = {
- num1: 2,
- num2: 3,
- add() {
- console.log(this.num1 + this.num2)
- // console.log(this)
- const $this = this
- function multiply() {
- console.log($this.num1 * $this.num2)
- }
- multiply()
- const division = () => {
- console.log(this.num2 / this.num1)
- }
- division()
- function subtraction(a, b) {
- console.log(this, a, b)
- console.log(this.num2 - this.num1)
- }
- return subtraction
- }
- }
- // console.log(calculator.add())
- const subs = calculator.add()
- const result = subs.bind(calculator, 1, 2) //don't call the function , just bind the 'this'
- result.apply('blabla') //don't work
- console.log(result())
- console.log(subs)
- // console.log(subs.call(calculator, 1, 2))
- // console.log(subs.apply(calculator, [1, 2]))
- //binding this to result
- //result
- //constructor function (this)
- //create empty object
- //return (this) object be default
- function Construct() {
- this.number1 = 1
- this.number = 2
- // console.log(this)
- }
- const numbersObj = new Construct()
- console.log(numbersObj)
- //this summary
- //1. global context indicates window obj
- //2. How you call the function will define How this indicates inside this function
- //3.plain function call -window object
- //(in strict mode undefined)
- //array function doesn't have own this binding(parent context defines 'this')
- //4. calling methods as a part of object this will indicate the object
- //5. constructor function this indicates empty object
- // const samim = {
- // firstName: 'samim',
- // lastName: 'hasan',
- // fullName() {
- // return this.firstName + this.lastName
- // }
- // }
- // const khalil = {
- // firstName: 'khalil',
- // lastName: 'hasan',
- // fullName() {
- // return this.firstName + this.lastName
- // }
- // }
- //factory function
- // function person(firstName, lastName) {
- // return {
- // firstName,
- // lastName,
- // fullName() {
- // return this.firstName + this.lastName
- // }
- // }
- // }
- // const samim = person('samim', 'Hasan')
- // const khalil = person('Khalil', 'Hasan')
- // console.log(khalil)
- function Person(firstName, lastName) {
- this.firstName = firstName
- this.lastName = lastName
- this.fullName = function () {
- return this.firstName + this.lastName
- }
- }
- const samim = new Person('samim', 'Hasan')
- const khalil = new Person('Khalil', 'Hasan')
- console.log(samim.fullName())
- console.log(khalil)
- //scope
- //var, let, const
- //var (global scope, functional scope)
- //let, const (global scope, block scope{})
- //scope , scope chian
- // var greet = 'Hello'
- // function showGreet() {
- // // var greet = 'Hola'
- // // console.log(greet)
- // function showHello() {
- // console.log(greet)
- // }
- // showHello()
- // }
- // showGreet()
- // console.log(greet)
- // const num1 = 0
- // {
- // const num2 = 2
- // console.log(num1)
- // {
- // const num3 = 3
- // console.log(num3)
- // console.log(num2)
- // }
- // }
- // console.log(num1)
- //closure (scope)
- function add() {
- const b = 5
- const a = 3
- function subtraction() {
- console.log(b - a)
- }
- return subtraction
- }
- const result2 = add()
- console.log(result2())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement