Java Script Object

Source: Internet
Author: User

Java Script Object

1. How to create

 1) Create in literal form

Example: var = stt{x:1,y:2,y:3};

or var = stt{x:1,

Y:2,

' For ': 3

Note Keywords must be placed in the middle of quotation marks

2) Create an object from new

Example: var = new STT ();

Stt.name = ' small fish ';

Stt.age = 20;

3) Creating an object from a constructor function

Example: Function test (name,age,sex) {

THIS.name = name;

This.age = age;

This.sex = sex; }

var STT = Test (Stt,20,nan);

4) through

2. Ways to Get Properties

   1) Object name. Property name Acquisition

Example: Alert (stt.name);

2) object name [' property name '] gets

Example: Alert (stt[' name ');

Note that when there is no quotation mark within [], it is not confined to the object itself but the entire program finds the property name.

3. Adding attributes

    1) Object name. property name = value;

Example Stt.add = ' Taiyuan ';

4. Delete Attributes

    1) Delete Object name. property name;

Example Delete stt.name;

5. Traversing properties

    1) for (var i in STT) {

Console.log (i)}

Note: This traversal knowledge outputs the name of the property in the object.

2) for (var i in STT) {

Console.log (Stt[i])}

Note: This traversal can only be used with [] to find the value of the property. Since the traversal of the resulting property name is string with quotation marks the result is STT. ' The name ' syntax is incorrect.

6. Can write method in object

Example: var = person{

Name = ' Set ';

Age = 20

sex = ' male ';

function speak () {

Console.log (' Speak ');}

}

Person.speak (); Calling methods in an object

          

Java Script Object

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.