1. What does the object contain?
In JavaScript, functions and strings are actually objects.
// Create an instance of the person object VaR Penson = {}; Person. getname = Function () {...}; Person. getage = Function (){...}; // Create an instance of an emloyee object VaR Employee = {}; Employee. gettitle = Function () {...}; Employ. getsalary = Function (){...}; // Inherit methods from person objects Employee. getname = Person. getname; employee. getage = Person. getage;
View code
- Understanding object members
Ads. addevent (window, 'load ',Function() {Alert ('Document. Body is a: '+Document. Body );});//Document. Body is a: Object htmlbodyelementAds. addevent (window,'Load ',Function() {Alert ('Document. getelementbyid is a: '+Document. getelementbyid );});//Document. getelementbyid is://Function getelementbyid () {[native code]}
View code
- Everything in the window object (covering objects in the scope chain)
<H1> override the alert () method
FunctionOverride (){//Override alert functionsVaRAlert =Function(Message) {window. Alert ('Overridden: '+Message) ;}; alert ('Alert');//Call the original Alert () function in the scope of the override () functionWindow. Alert ('window. Alert');} Override ();//Call the original Alert () in the window scope ();Alert ('alert from outside ');
View code
// Pop-up sequence: // overridden: Alert // window. Alert // alert from outside
- Understanding scopes and closures is fundamental
2. Create your own object
VaRMyobject =NewObject ();//An object is an object.VaRMyobject ={};//These two types have been instantiated. The instantiated object must be a constructor.//Create an arrayVaRMyarray =NewArray ();VaRAnotherobject =NewMyobject ();//Incorrect
- More and more: Create Constructor
Function Myconstructor (){ // Somecode } VaR Myconstructor = Function (){ // Comecode } VaR Myconstructor = New Function ('A', '/* somecode */') // Performance problems // Use VaR Myobject = New Myconstructor (); // For example: Function Myconstructor (Message) {alert (Message ); This . Mymessage = Message ;} VaR Myobject = New Myconstructor ('instantiating myobject' ); VaR Message = myobject. mymessage;
// Understanding the differences between instances and constructors helps eliminate many existing problems // ------- Only objects can run normally // Create an object instance VaR Myobject = New Object (); // Add an attribute Myobject. Name = 'jeff' ; // Add a method Myobject. alertname =Function () {Alert ( This . Name );} // Execute the Add Method Myobject. alertname (); // Create an instance of a function object VaR Myconctructor = Function (){ // Somecode } // Add a static property Myconstructor. Name = 'jeff' ; Myconstructor. alertname = Function () {Alert ( This . Name);} myconstructor. alertname (); // However, name and alertname will not be in the new instance VaR Anotherexample = New Myconstructor (); anotherexample. alertname (); // Not a function Error
- Add static methods to the prototype
//Create ConstructorFunctionmyconstrctor (Message) {alert (Message );This. Mymessage =Message ;}//Add a public method (add at the underlying layer)Myconstructor. Prototype. clearmessage =Function(String ){This. Mymessage + = ''+String ;}//You can call this method on a new instance.VaRMyobject =NewMyconstructor ('Hello world! '); Myobject. clearmessage ();
Control access through private and privileged members (you only need to use common VaR and fuction keywords in this constructor)
// Create Constructor Function Myconstrctor (Message ){ This . Mymessage = Message; // Private attributes VaR Separator = '-' ; VaR Myowner = This ; // Private Method Function Alertmessage () {alert (myowner. Message );} // Information is displayed during instantiation; Alertmessage ();} // If the following method is used, an error occurs. Myconstructor. Prototype. appendtomessage = Function (String ){ This . Mymessage + = seqarator + string; // Error undefined } // The privileged method must be defined using the this keyword in the scope of the constructor. // Create Constructor Function Myconstrctor (Message ){ This . Mymessage = Message; // Private attributes VaR Separator = '-' ; VaR Myowner = This ; // Private Method Function Alertmessage () {alert (myowner. Message );} // Information is displayed during instantiation; Alertmessage (); // Privileged Methods This . Appendtomessage = Function (String ){ This . Mymessage + = separator + String; alertmessage ();}} // At this time, you can VaR Myobject = New Myconstructor ('Hello world' ); Myobject. appendtomessage ( 'Jeff' ); // However, you still cannot use the following method: Myobject. alertmessage (); // Not found
- Are common, Private, privileged, and static Members really so important?
// Create Constructor Function Myconstrctor (Message ){ This . Mymessage = Message; // Private attributes VaR Separator = '-' ; VaR Myowner = This ; // Private Method Function Alertmessage () {alert (myowner. Message );} // Information is displayed during instantiation; Alertmessage (); // Privileged Methods This . Appendtomessage = Function (String ){ This . Mymessage + = separator + String; alertmessage ();}} // Common Methods Myconstructor. Prototype. clearmessage = Function (String ){ This . Mymessage ='' ;} // Static attributes Myconctructor. Name = 'jeff' ; // Static Method Myconstructor. alertname = Function () {Alert ( This . Name );}
Remember the rules for a few days to ensure that you properly define the identities of all members:
- Because private and privileged members are inside the function. New words are taken to every instance of the function.
- A common prototype member is part of an object blueprint and is applicable to each instance of the object instantiated using the new keyword.
- Static members only use one special instance of an object.