In-depth analysis of object concepts in JavaScript programming, in-depth analysis of javascript

Source: Internet
Author: User

In-depth analysis of object concepts in JavaScript programming, in-depth analysis of javascript

Everything in JavaScript is an object: String, value, array, function...
In addition, JavaScript allows custom objects.
All things are objects
JavaScript provides multiple built-in objects, such as String, Date, and Array. Objects are only special data types with properties and methods.

  • A boolean can be an object.
  • A numeric type can be an object.
  • A string can also be an object.
  • Date is an object
  • Math and regular expression are also objects
  • An array is an object.
  • Even functions can be objects.

JavaScript Object

Object is only a special type of data. The object has attributes and methods.
Access Object Attributes
An attribute is an object-related value.
The syntax for accessing object properties is:

objectName.propertyName


In this example, The length attribute of the String object is used to obtain the length of the String:

var message="Hello World!";var x=message.length;


After the preceding code is executed, the value of x is:

12

Method of accessing objects
A method is an action that can be executed on an object.
You can use the following syntax to call a method:

objectName.methodName()


In this example, the toUpperCase () method of the String object is used to convert the text to uppercase:

var message="Hello world!";var x=message.toUpperCase();


After the preceding code is executed, the value of x is:

HELLO WORLD!

Create a JavaScript Object
With JavaScript, you can define and create your own objects.
There are two different ways to create a new object:

  1. Define and create an instance of an object
  2. Define an object using a function, and then create a new object instance.

Create a direct instance
In this example, a new instance of the object is created and four attributes are added to it:
Instance

person=new Object();person.firstname="John";person.lastname="Doe";person.age=50;person.eyecolor="blue";


Alternative syntax (Use object literals ):
Instance

person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};


Use object constructor
This example uses functions to construct objects:
Instance

function person(firstname,lastname,age,eyecolor){this.firstname=firstname;this.lastname=lastname;this.age=age;this.eyecolor=eyecolor;}

In JavaScript, this usually points to the function we are executing, or to the object to which the function belongs (runtime)
Create a JavaScript object instance
Once you have an object constructor, you can create a new object instance like this:

var myFather=new person("John","Doe",50,"blue");var myMother=new person("Sally","Rally",48,"green");

Add property to JavaScript Object
You can add new attributes to an existing object by assigning values to the object:
Assuming that personObj already exists-you can add these new attributes for it: firstname, lastname, age, and eyecolor:

person.firstname="John";person.lastname="Doe";person.age=30;person.eyecolor="blue";x=person.firstname;


T after the above code is executed, the value of x will be:

John

Add a method to a JavaScript Object
The method is only a function attached to an object.
Methods for defining objects within the constructor function:

function person(firstname,lastname,age,eyecolor){this.firstname=firstname;this.lastname=lastname;this.age=age;this.eyecolor=eyecolor;this.changeName=changeName;function changeName(name){this.lastname=name;}}

The value of changeName () function name is assigned to the lastname attribute of person.
Now you can try:

myMother.changeName("Doe");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.