JavaScript Basic Learning (iv)-object

Source: Internet
Author: User

First, the basic operation of object 1. Creation of objects

In JavaScript, you create objects in two ways: constructors and object literals .

(1) Constructors

var person = new Object ();p erson.name = "Tom";p erson.age = 22;person.say = function () {alert ("Hello");}

(2) object literal

var person = {Name: "Tom", age:22}

2. Deletion of object properties and methods

You can use the delete operator.

var person = {};p erson.name = "Tom";p erson.age = 20;person.say = function () {alert ("Hello!!!");} The delete operator deletes the properties and methods of the object delete Person.name;delete Person.say;alert (person.age); 20alert (Person.name); Undefinedalert (Person.say); Undefined

3. Traversal of objects

You can use the for in enumeration to iterate.

var person = {};p erson.name = "Tom";p erson.age = 20;person.say = function () {alert ("Hello!!!");} Traverse the object for (var attr in person) {alert (attr + "=" + Person[attr]);}

Ii. properties and methods of object 1.constructor properties

The constructor property is the constructor that holds the current object, and in the previous example, constructor saves the object method.

var person = {};p erson.name = "Tom";p erson.age = 20;person.say = function () {alert ("Hello!!!");} /* * Constructor: Save object Creation Function * Functions object () {     *[native code] *} */alert (Person.constructor)

JavaScript Basic Learning (iv)-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.