"Everything" in JavaScript is an Object: a String, a Number, an Array, a Function....
In addition, JavaScript allows you to define your own objects.
JavaScript has several built-in objects, like String, Date, Array, and more.
An object is just a special kind of data, with properties and methods.
Properties are the values associated with an object.
The syntax for accessing the property of an object is:
This example uses the length property of the String object to find the length of a string:
The value of x, after execution of the code above will be:
Methods are the actions that can be performed on objects.
You can call a method with the following syntax:
This example uses the toUpperCase() method of the String object, to convert a text to uppercase:
The value of x, after execution of the code above will be:
With JavaScript you can define and create your own objects.
There are 2 different ways to create a new object:
This example creates a new instance of an object, and adds four properties to it:
Alternative syntax (using object literals):
This example uses a function to construct the object:
The reason for all the "this" stuff is that you're going to have more than one person at a time (which person you're dealing with must be clear). That's what "this" is: the instance of the object at hand.
Once you have a object constructor, you can create new instances of the object, like this:
You can add new properties to an existing object by simply giving it a value.
Assume that the personObj already exists - you can give it new properties named firstname, lastname, age, and eyecolor as follows:
The value of x, after execution of the code above will be:
Methods are just functions attached to objects.
Defining methods to an object is done inside the constructor function:
The changeName() function assigns the value of name to the person's lastname property.
JavaScript knows which person you are talking about by "substituting" this with myMother.
JavaScript is an object oriented language, but JavaScript does not use classes.
In JavaScript you don’t define classes and create objects from these classes (as in most other object oriented languages).
JavaScript is prototype based, not class based.
The JavaScript for...in statement loops through the properties of an object.
Note: The block of code inside of the for...in loop will be executed once for each property.
Looping through the properties of an object:
The perfect solution for professionals who need to balance work, family, and career building.
More than 10 000 certificates already issued!
The HTML Certificate documents your knowledge of HTML.
The HTML5 Certificate documents your knowledge of advanced HTML5.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
Your message has been sent to W3Schools.