A detailed description of JavaScript objects

Source: Internet
Author: User

Tag: Indicates select Alert No application developed his field obj

An overview of JavaScript objects

What is an object is, in fact, a type, that is, a reference type. The value of the object is an instance of the reference type.

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

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

It is an ideal choice for storing and transferring data in an application.

  

How to create a JavaScript object-Create an object using the new operator

functionObjrun () {return' 123 ';}varbox =NewObject ();//An object was createdAlerttypeofbox);//object type is objects   //Create an attribute field for an objectBox.name = ' Li Tinghui '; Box.age= 28; Box.run=Objrun (); Alert ("My name is" +box.name+ ", I Am" +box.age+ "year old ...");//Get property fields for an objectalert (Box.run);//calling methods in an object

The new keyword can be omitted

var // The New keyword is omitted = ' Huang Jia ai '= A,alert ("My name is" +box.name+ ", I this year" +box.age+ "years old ...");     

How JavaScript objects are created-using literal notation

var box = {    // create attribute field    age:28,    function//  The method in the object        return ' run ';    }; Alert ("My name is" +box.name+ ", I Am" +box.age+ "year old ..."); Alert (Box.run ()); // calling methods in an object

Property fields can also be used as String forms

var box = {    // create attribute field    ' age ':};alert ("My name is" +box.name+ ", I'm this year" +box.age+ "years old .....");

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

var box = {};     // literal method declares an empty object // traditional way to create field properties for an object box.age =;       // There are two ways to output a property // dot notation output alert ("My name is" +box[' name ']+ ", I am this year" +box[' age ']+ "years old ..."); // bracket notation Output, note the need to enclose the quotation marks

How JavaScript objects are created-display constructors to create objects

Whenever a function is used by the new operator, the function can become a constructor

var function () {  this. Name = "O2";  }  var New M (); alert (o2.name);

How to create a JavaScript object-object.create ()

var p = {    name:"O3"}; var o3 = object.create (p) alert (o3.name);

Passing an object as a parameter to a function

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

A detailed description of JavaScript objects

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.