What underlies this JavaScript idiom: var self = this?

前端 未结 10 1889
长情又很酷
长情又很酷 2020-11-22 03:37

I saw the following in the source for WebKit HTML 5 SQL Storage Notes Demo:

function Note() {
  var self = this;

  var note = document.createElement(\'d         


        
10条回答
  •  不思量自难忘°
    2020-11-22 04:38

    function Person(firstname, lastname) {
      this.firstname = firstname;
    
      this.lastname = lastname;
      this.getfullname = function () {
        return `${this.firstname}   ${this.lastname}`;
      };
    
      let that = this;
      this.sayHi = function() {
        console.log(`i am this , ${this.firstname}`);
        console.log(`i am that , ${that.firstname}`);
      };
    }
    
    let thisss = new Person('thatbetty', 'thatzhao');
    
    let thatt = {firstname: 'thisbetty', lastname: 'thiszhao'};
    

    thisss.sayHi.call(thatt);

提交回复
热议问题