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