JavaScript Learning Note three: JavaScript objects

Source: Internet
Author: User

First, preface

  • Basic concepts of Objects : JavaScript provides multiple built-in objects, such as String, Data, array, and so on. Object refers to a special data type with properties and methods
  • To create an object: There are two main ways to create a JavaScript object: First, define an instance of the created object, and second, use a function to define the object, and then create a new object
  • <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head></Head><Body><Script>    //Create a direct instancepersion= NewObject (); Persion.firstname= "Bill"; Persion.lastname= "Gates"; Persion.age=  About; Persion.eyecolor= "Blue"; document.write (Persion.firstname+ " is" +Persion.age+ "years old.<br/>"); //alternative syntax (using object literals)persion={firstname:"John", LastName:"Doe", Age: -, Eyecolor:"Blue"}; document.write (Persion.firstname+ " is" +Persion.age+ "years old.<br/>"); //constructing objects using constructors    functionPersionv (firstname,lastname,age,eyecolor) { This. FirstName=FirstName;  This. LastName=LastName;  This. Age=Age ;  This. Eyecolor=Eyecolor; //add a method to a JavaScript object         This. ChangeName=ChangeName; functionChangeName (name) { This. LastName=name; }} myfather=  NewPersionv ("Bill","GateV1", the,"Red"); document.write (Myfather.firstname+ " is" +Myfather.age+ "years old.<br/>"); Myfather.changename ("hehehe"); document.write (Myfather.firstname+ " " +Myfather.lastname+ " is" +Myfather.age+ "years old.<br/>");</Script></Body></HTML>

  • access the properties of an object, which refers to an object-related value, and the syntax for accessing an object's properties is as follows:
    Objectname.propertyname

    Specific examples:

  • <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head></Head><Body><Script>    varmessage= "Hello world!"; varx=message.length; document.write (x)</Script></Body></HTML>

  • methods for accessing objects : Methods are actions that can be performed on an object, with the following syntax:
  • Objectname.methodname ()

    Specific examples:

  • <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head></Head><Body><Script>    varmessage= "Hello world!"; varx=message.touppercase (); document.write (x)</Script></Body></HTML>

JavaScript Learning Note three: 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.