1. Object description
An object is a special type of data that is encapsulated by attributes and methods
Property refers to the value associated with the object: the name of the object. Property name
A method refers to the behavior that an object can perform or a function that can be accomplished: the object name. Method Name ()
Defining objects
Create an instance of an object
Create a template for an object
Using JSON
2. Create a Generic Object
To create a common object by using object objects
function Testobject () {varpersonobj=NewObject (); //Add PropertyPersonobj.name="John"; Personobj.age= -; //Add Methodpersonobj.say=NewFunction ("alert (' hello! ');"); //TestPersonobj.say (); alert (personobj.age);}
3. Creating templates for objects
Define constructors to create custom objects
Syntax: function objname (parameter 1, parameter 2,...) {}
function Person (n,a) {//defining the name and age properties This. name=N; This. age=A; //Definition Method ShowName This. showname=function () {alert ("My Name is"+ This. Name); }; //Definition Method Introduceself This. introduceself =Introfunc ();} function Introfunc () {alert ( This. name+":"+ This. age);}
// Test Object function Test () { var o1=New person ("Mary", ); alert (o1.age); O1.showname (); O1.introduceself (); var o2=New person ("John",+); alert (o2.age); O2.showname (); O2.introduceself ();}
4. JSON
JSON (JavaScript Object Notation) is a lightweight data interchange format
Define by using name/value pairs
The name needs to be caused by ""
Use between many pairs of definitions, separating
The name can be a property
The property value of the string type, which needs to be caused by the ""
var obj={ "name":"Jerry", "Age":; // test Object alert (obj.name);
Object-Oriented Fundamentals