JavaScript objects are collections of key or value pairs. The values consist of properties and methods. Also, it contains other JavaScript data types, such as strings, numbers, and Booleans. In this article, we will discuss about the different JavaScript methods in the following sequence:
What are JavaScript Methods?
JavaScript methods are actions that can be performed on objects. A JavaScript method is a property that contains a function definition. For example:
Property | Value |
FirstName | Daisy |
LastName | Green |
Age | 25 |
FullName | function() {return this.FirstName + ” ” + this.LastName;} |
These methods are nothing but functions stored as object properties. Now let’s see how you can access these object methods in JavaScript.
How to access object Methods?
You can access the object methods using the following syntax:
objectName.methodName()
Here, you have to describe the FullName() as a method of the Person object, and FullName as a property. The fullName property works as a function when it is invoked with (). Here is an example of how to accesses the FullName() method of a person object:
Name = person.FullName();
This is how you can access the object method. Now, there are different types of Methods. So, we will discuss these methods in detail.
Different Types of JavaScript Methods
The different types of JavaScript Methods that are available in global Object constructor are:
- Object.create()
- Object.keys()
- Object.freeze()
- Object.values()
Object.create
You can create object with Object.create() function. This has an additional flexibility that lets you choose the prototype of your new object.
let createObj = Object.create(obj); console.log(createObj); //{} createObj.name = "Danny"; console.log(createObj.speak());
In the above example, obj is the prototype from which createdObj is created. Also, it can use the properties of the prototype due to inheritance. Thus, you can use speak() method without declaring that in createdObj.
Object.keys
The object.keys function is used to pick only keys or property labels of objects and returns an array.
let keys = Object.keys(person); console.log(keys); // [ 'name', 'age' ]
Object.freeze
The freeze function is used to freeze the object for any changes in key or values. It doesn’t throw any error unless you are in strict mode. But there will be no effect of value change on your object.
let frozenObject = Object.freeze(person); frozenObject.name = "Rachel"; console.log(frozenObject);
Object.values
This function is used to select only values of objects and returns an array in the following way:
let values = Object.values(person); console.log(values);
These are some of the different types of Methods. With this, we have come to the end of our article. I hope you understood the different types of JavaScript methods and how they are used.
Now that you know about Methods in JavaScript, check out the Web Development Certification Training by Edureka. Web Development Certification Training will help you learn how to create impressive websites using HTML5, CSS3, Twitter Bootstrap 3, jQuery and Google APIs and deploy it to Amazon Simple Storage Service(S3).
Got a question for us? Please mention it in the comments section of “JavaScript Methods” and we will get back to you.