Objects in JavaScript (i)

Source: Internet
Author: User

JavaScript is a programming language based on the object model design, and its core (ECMAScript) has a very powerful and flexible object-oriented programming capability.

  1. What is an object?

    In JavaScript, an object is an independent entity that has properties and types. ECMA-262 defines an object as an unordered collection of properties, each of which holds an original value, object, or function.

    Let's compare it with a cup. A cup is an object (object) that has properties. The cup has color, pattern, weight, what material make up and so on. Similarly, a JavaScript object has properties to define its characteristics. Cups also have certain functions, such as the ability to hold a quantity of liquid. Similarly, a JavaScript object has a way to define its functionality.

    All native types are null treated as objects, except and in addition to undefined each other. They can be given attributes (some types of assigned properties cannot be persisted), and they all have all the characteristics of the object.

  2. What are the objects of JavaScript?

    JavaScript has a set of predefined objects and some of the objects provided by the host app, and here we only introduce JavaScript built-in objects, which can be divided into categories as follows:

    • Global constants: Infinity, NaN, undefined, null
    • Global methods: eval (), Isfinite (), IsNaN (), parsefloat (), parseint (), decodeURI (), decodeURIComponent (), encodeURI (), encodeURIComponent ()
    • Basic objects: Object, Function, Boolean, Error,
    • Numeric Date: number, Math, date
    • Word processing: String, REGEXP
    • Array Collections: Array

    Of course, users can also create objects.

  3. How are objects defined in JavaScript?

    From JavaScript 1.2, you can create an object from the object initializer (Initializer). Alternatively, you can create a constructor and use the function and the new operator to initialize the object.

    • Using the object initializer

      The syntax for creating objects through the object initializer is as follows:

      var obj = {property_1:   value_1,   // property_# May is an identifier ...            2:   value_2,   //  or a number ...            //  ...,            // or a string

      Here obj is the name of the new object, each of which property_i is an identifier (which can be a name, a number, or a string), and each value_i is an expression whose value will be given to Property_i . objand assignment is optional; If you don't need to reference the object elsewhere, you don't need to assign it to a variable. (Note that where you accept a statement, you may want to enclose the object definition text in parentheses to avoid confusing the text with the block code)

      Object initializers are expressions, and new objects are created where these expressions appear, so that even objects that are created by the exact same object initialization expression are not equal. It's like invoking new object () to create different entity entities.

      The following is an attribute that creates a cup object with three properties. We notice that property design is also an object with attributes.

        {color: " white ", 
             
              // 
              default value of properties  weight:0.5
              , Material:  "China" 
              " Pink ", Mark:  "Rose"  ", fill:  function   (liquid, volumn) { //
             todo  Console.log ("the" + this . Material + "Cup is filled with" + volumn + "litre" + liquid + "."        ); }    }

      In JavaScript version 1.1 and earlier, we cannot use object initializers to create objects, only the following methods are used to create objects.

    • To create an object using a constructor function

      You can create objects in two steps:

        • Define the type of the object by creating a constructor function. Capitalization is a very common and appropriate method of usage.
        • Creates an object instance from new.

      To define an object type, create a function for the object type to declare the name, properties, and methods of the type. For example, to create a cup object, we can use the following function:

          functionCup (color, weight, material, Designcolor, markimage) { This. color =color;  This. Weight =weight;  This. Material =material;  This. Design ={color:designcolor, mark:markimage};  This. Fill =function(liquid, volumn) {//TodoConsole.log ("the" + This. Color + "and" + This. Material + "Cup is filled with" + volumn + "litre" + liquid + "."); }    }

      Now you can create an object like this mycup :

      var New Cup ("White", 0.5, "China", "pink", "Rose");

      It's time to createmycup 并且将指定的值赋给它的属性。因而 mycup.color 的值是字符串 "whhite", mycar.weight 的值是0.5(此处省略重量单位),依此类推。

      You can new create any number of objects by calling car  . For example:

      var New Car ("Red", 0.4, "plastic", "Blue", "Girl"varnew Car ("Grown", 0.4, "glass", "green", "tree");
    • Using Object.create (ECMASCRIPT5 and later support)

      Objects can also be Object.create created using methods. This method is useful because it allows you to select its prototype object for the object you are creating, without having to define a constructor.

      Object.create (proto[, Propertiesobject])

      Proto refers to the prototype object of the object being created, and Propertiesobject is an object value that can contain several properties.

          varCup ={color:"White",//default value of propertiesweight:0.5, Material:"China", Design: {color:"Pink", Mark:"Rose"}, fill:function(liquid, volumn) {//TodoConsole.log ("the" + This. Color + "and" + This. Material + "Cup is filled with" + volumn + "litre" + liquid + "."); }    }    //Create new Cup type called Mycup    varMycup =object.create (Cup); Mycup.fill ("Water", 0.2);//Output:the White and the China Cup are filled with 0.2 litre water.    varLilycup =object.create (Cup); Lilycup.color= "Red"; Lilycup.material= "Plastic"; Lilycup.fill ("Apple juice", 0.3);//output:the Red and plastic cup is filled with 0.3 litre apple juice.
  4. How do I use objects?

    Once the good one object is built, we can access the properties and methods of the object:

    ObjectName.propertyNameobjectName.methodName ();

    About this use we have used in the above sample, for example

    Lilycup.material = "plastic";

    Lilycup.fill ("apple juice", 0.3); 。

There are several main methods of object inheritance and objects that we will cover in the next article.

Objects in JavaScript (i)

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.