JavaScript Basics Blind Spot Summary--object

Source: Internet
Author: User

First, define the basic format of the object:

var hero = {breed: ' Turtle ', Occupation: ' Ninja ', talk:function () {    alert ("a!!");    } Author: {    firstname: ' Martin ',    LastName: ' Lu '   }}

Property names are not quoted, but they need to be quoted when the property name has reserved words or special characters (spaces, etc.)

Second, the operation of the object properties

You can add/remove properties or methods for an object at any time:

var hero = {};hero.breed = ' turtle '; hero.name = ' Alice '; hero.sayname = function () {return hero.name;}

In an object, this refers to this object, such as the following method of invocation:

var hero = {name: ' Martin ', sayname:function () {    return this.name;}} Hero.sayname ();

Third, use the constructor function to create the object

You can accept some parameters when you create an object, and use the New keyword to create an object

function Hero (name) {this.name = Name;this.occupation = ' Ninja '; this.whoareyou = function () {    return "I am" + this.name +this.occupation;}} var hero = new Hero (' Martin ');

Four, constructor properties

When an object is created, the object automatically generates a constructor property that points to a reference to the constructor function that is used to create the object.

Like Hero.constructor, the return is Hero (name)

You can use this property to create another new object, as shown in the following example:

var hero1 = new Hero.constructor (' Martin ');

Note that the above code can be used to create new objects in this way even if the hero object was not previously created.

If the object was created by using text notation, it is created by the built-in function constructor, object (), as follows:

var o = {};

O.constructor;

>>>object ()

typeof O.constructor;

>>> "function"


V. Create an object using a function that returns an object

You can create an object using a function that returns an object:

function Factory (name) {return {    name:name;}} var o = Factory ("Martin"); o.name;   return to Martino.constructot;   Return Object ()

This problem occurs when a function that returns an object creates an object with new: The object created by new (returned by the constructor) is no longer a factory object, but an object that contains the Name property

JavaScript Basics Blind Spot Summary--object

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.