Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Bootcamps Get Certified Upgrade Teachers Spaces Bootcamps
   ❮     
     ❯   

Basic JavaScript

JS TutorialJS SyntaxJS VariablesJS OperatorsJS If ConditionsJS LoopsJS StringsJS NumbersJS FunctionsJS ObjectsJS ScopeJS DatesJS Temporal DatesJS ArraysJS SetsJS MapsJS IterationsJS MathJS RegExpJS DestructuringJS Data TypesJS ErrorsJS DebuggingJS ConventionsJS ReferencesJS 2026JS Versions

JS HTML

JS HTML DOMJS EventsJS ProjectsNew

JS Advanced

JS FunctionsJS ObjectsJS ClassesJS AsynchronousJS ModulesJS Meta & ProxyJS Typed ArraysJS DOM NavigationJS WindowsJS Web APIsJS AJAXJS JSONJS jQueryJS GraphicsJS ExamplesJS Reference


JavaScript Object Methods

What are Object Methods?

Methods areactions that can be performed on objects.

Methods arefunctions stored asproperty values.

Example

constperson = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  fullName: function() {
    returnthis.firstName + " " +this.lastName;
  }
};
Try it Yourself »
PropertyValue
firstNameJohn
lastNameDoe
age50
fullNamefunction() {
  return this.firstName + " " + this.lastName;
}

Thethis Keyword

In an object method,this refers to the object.

Example 1

const person = {
  firstName: "John",
  lastName: "Doe",
  id: 5566,
  getId: function() {
    returnthis.id;
  }
};

let number = person.getId();
Try it Yourself »

In the example above,this refers to theperson object.

this.id means theid property of theperson object.

Example 2

constperson = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  fullName: function() {
    returnthis.firstName + " " +this.lastName;
  }
};
Try it Yourself »

In the example above,this refers to theperson object.

this.firstName means thefirstName property of theperson object.

this.lastName means thelastName property of theperson object.


Accessing Object Methods

Tocall an object method, addparentheses ():

Without parentheses you get the function itself.

Syntax

objectName.methodName()

If you call a methodwith parentheses, it willexecute as a function:

Example

name = person.fullName();
Try it Yourself »

If you call a methodwithout parentheses, it will return thefunction definition:

Example

name = person.fullName;
Try it Yourself »


Adding a Method to an Object

You can add a method to an object byassigning a function to a property:

Example

// Assign person.name to a function
person.name = function () {
  return this.firstName + " " + this.lastName;
};
Try it Yourself »

In the example above,person.name is a property with a function assigned to it.


Adding a JavaScript Method

This example uses the JavaScripttoUpperCase() method to convert a text to uppercase:

Example

person.name = function () {
  return (this.firstName + " " + this.lastName).toUpperCase();
};
Try it Yourself »

Summary

  • Methods are functions stored asobject properties
  • Call a method with parentheses:person.fullName()
  • In methods,this refers to the object
  • You canadd methods to objects by assigning afunction to a property






×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.

-->
[8]ページ先頭

©2009-2026 Movatter.jp