Overview of JavaScript objects

Source: Internet
Author: User

An overview of JavaScript objects:

  1, what is an object, is actually a type, that is, the reference type. The value of the object is an instance of the reference type.

2. A reference type in JavaScript is a data structure used to organize data and functionality together. It is also often called a class, but there is no such thing in JavaScript (impersonation).

3. Although JavaScript is an object-oriented language, it does not have the basic structure of classes and interfaces supported by traditional object-oriented languages.

4, in the application of storage and transfer of data, it is a very ideal choice.

  

Second, the creation of JavaScript objects:

  There are two types of Object creation. One is the use of the new operator, and the literal notation.

1. Create an object using the new operator

<script type= "Text/javascript" >   varnew Object ();        // an object   was created Alert (typeof box);            // Object Type  is objects      //  Create an attribute field for an object] Box.name = ' Li Tinghui ';                =; Alert (// Get object's attribute field </script>

The new keyword can be omitted

<script type= "Text/javascript" >    var//          omit the new keyword = ' huang Jia Ai '    = a; Alert ("My name is" +box.name+ ", I Am" +box.age+ "year old ..."); </script>

    2. Create objects using literal methods

<script type= "Text/javascript" >    var box =// create attribute field age:28  }; Alert ("My name is" +box.name+ ", I Am" +box.age+ "year old ..."); </script>

Property fields can also be used as String forms

<script type= "Text/javascript" >    var box = {  // create attribute field ' age ':  ; Alert ("My name is" +box.name+ ", I Am" +box.age+ "year old ..."); </script>

Create objects by using literal methods, but you can use the traditional way when creating fields for objects

<script type= "Text/javascript" >   var box = {};     // literal method declares an empty object  // traditional way to create field properties for an object box.age =;             alert (box.age); Alert ("My name is" +box.name+ ", I Am" +box.age+ "year old ..."); </script>

There are two ways to output a property

// dot notation Output // bracket notation Output, note the quotation marks

Iii. Creating methods for objects

 var  box = {run:  function  () {//  method in Object  return  ' run ' ; }} alert (Box.run ());  //  call method in Object  
function Objrun () {       return ' 123 ';    }    var New Object ();    = ' Euna lee '   =;    = Objrun ();      alert (box.age);   alert (box.run);

In the actual development process, we generally prefer the literal declaration method. Because it is clear, the syntax code is few, but also gives the person a kind of encapsulation feeling. Literals are also the preferred way to pass a large number of optional parameters to a function.

function // parameter is an object if  (obj.name!=// determines if the attribute exists  if (obj.age! = undefined) alert (obj.age); }  var obj=' Li Tinghui ' , Box (obj);        

Delete Object properties using Delete

    var box == ' Orient card ';  } alert (Box.name);   Delete  box.name; alert (box.name); // has been deleted, undefined

Four, the object analysis:

  1. What elements are included in the object

properties (Fields) method (function)

<script type= "Text/javascript" >varbox =NewObject ();//An object was createdAlerttypeofbox);//object type is objects      //create an object's properties (field)Box.name = ' Li Tinghui '; Box.age= 28; //Methods for creating objects (functions)    functionShow () {return"My name is" +box.name+ ", I Am" +box.age+ "year old .....; } alert (Show ()); //Get property fields for an object</script>

Simulate the map collection using the literal method:

             var oMap = {                8: "Xiao Qiang", 3: "Wang Choi", 7: "Xiao Ming"            }//            var val1 = omap[8]; //            alert ("Val1:" +val1);                        var val2 = Get (7); //             alert ("Val2:" +val2);                        function get (key) {                return  Omap[key];            }

An overview of JavaScript objects

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.