[ Web Proxy ]
URL:
Viewing: https://ja.javascript.info/object-methods [Back]  [Original]

, "this"
:
Light themeDark theme
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek

, "this"

:

let user = {
  name: "John",
  age: 30
};

JavaScript

user Hello :

let user = {
  name: "John",
  age: 30
};

user.sayHi = function() {
  alert("Hello!");
};

user.sayHi(); // Hello!

user.sayHi

user.sayHi() !

user sayHi

:

let user = {
  // ...
};

// 
function sayHi() {
  alert("Hello!");
};

// 
user.sayHi = sayHi;

user.sayHi(); // Hello!

(object-oriented programming), OOP

OOP E.Gamma, R.Helm, R.Johnson, J.Vissides Design Patterns: Elements of Reusable Object-Oriented Software G.Booch Object-Oriented Analysis and Design with Applications

:

// 

let user = {
  sayHi: function() {
    alert("Hello");
  }
};

// 
let user = {
  sayHi() { // "sayHi: function()" 
    alert("Hello");
  }
};

"function" sayHi()

()

this

user.sayHi() user

this

this

:

let user = {
  name: "John",
  age: 30,

  sayHi() {
    // "this"  ""
    alert(this.name);
  }

};

user.sayHi(); // John

user.sayHi() this user

this :

let user = {
  name: "John",
  age: 30,

  sayHi() {
    alert(user.name); // "this"  "user"
  }

};

user admin = user user

:

let user = {
  name: "John",
  age: 30,

  sayHi() {
    alert( user.name ); // 
  }

};


let admin = user;
user = null; // 

admin.sayHi(); // Whoops! sayHi() ! !

alert user.name this.name

this

JavaScript this

:

function sayHi() {
  alert( this.name );
}

this

2 "this ":

let user = { name: "John" };
let admin = { name: "Admin" };

function sayHi() {
  alert( this.name );
}

// 2
user.f = sayHi;
admin.f = sayHi;

//  this 
//  "this"  "" 
user.f(); // John  (this == user)
admin.f(); // Admin  (this == admin)

admin['f'](); // Admin ()

obj.f() f this obj user admin

: this == undefined

:

function sayHi() {
  alert(this);
}

sayHi(); // undefined

strict this undefined this.name

strict ( use strict ) this ( window) "use strict"

this this

this

this this

JavaScript this

this 1

this

this this

arrow() user.sayHi() this :

let user = {
  firstName: "Ilya",
  sayHi() {
    let arrow = () => alert(this.firstName);
    arrow();
  }
};

user.sayHi(); // Ilya

this

  • object.doSomething()
  • this

this

  • this this
  • object.method() this object

this this

: 5

makeUser

ref

function makeUser() {
  return {
    name: "John",
    ref: this
  };
};

let user = makeUser();

alert( user.ref.name ); // 

:

:

function makeUser() {
  return {
    name: "John",
    ref: this
  };
};

let user = makeUser();

alert( user.ref.name ); // Error: Cannot read property 'name' of undefined

this

makeUser() this undefined

this this

ref: this this

:

function makeUser() {
  return {
    name: "John",
    ref() {
      return this;
    }
  };
};

let user = makeUser();

alert( user.ref().name ); // John

user.ref() this .

: 5

3 calculator :

  • read() 2
  • sum()
  • mul()
let calculator = {
  // ... your code ...
};

calculator.read();
alert( calculator.sum() );
alert( calculator.mul() );

let calculator = {
  sum() {
    return this.a + this.b;
  },

  mul() {
    return this.a * this.b;
  },

  read() {
    this.a = +prompt('a?', 0);
    this.b = +prompt('b?', 0);
  }
};

calculator.read();
alert( calculator.sum() );
alert( calculator.mul() );

: 2

ladder :

let ladder = {
  step: 0,
  up() {
    this.step++;
  },
  down() {
    this.step--;
  },
  showStep: function() { // 
    alert( this.step );
  }
};

:

ladder.up();
ladder.up();
ladder.down();
ladder.showStep(); // 1

up down :

ladder.up().up().down().showStep(); // 1

JavaScript

let ladder = {
  step: 0,
  up() {
    this.step++;
    return this;
  },
  down() {
    this.step--;
    return this;
  },
  showStep() {
    alert( this.step );
    return this;
  }
}

ladder.up().up().down().up().down().showStep(); // 1

11:

ladder
  .up()
  .up()
  .down()
  .up()
  .down()
  .showStep(); // 1
let ladder = {
  step: 0,
  up: function() {
    this.step++;
    return this;
  },
  down: function() {
    this.step--;
    return this;
  },
  showStep: function() {
    alert(this.step);
  }
};


Web Proxy Viewer  |  New URL  |  Original Page