How JavaScript implements "object-oriented"

zhaozj2021-02-16  95

How JavaScript implements "object-oriented"

JavaScript is an object-based language that can only use the built-in objects provided by the system. How can JavaScript write the same code as an object language? Let me talk about my method for everyone.

1. Define a class (class "// *********** code begin *********** / / define class function myclass () {this.field1; / / Define a member this.field2;} var obj = new myclass (); // call class OBJ.AA = 1; // Access member obj = null; // Release object // ******* *** Code end ***********

2. Define the method (Method) // *********** Code Begin ******************* Function myclass () {this.method1 = _func1; this.method2 = _func2;

// Function function _func1 (aa) {// code here}} function _func2 (aa, bb) // external function {// code heren} var obj = new myclass (); // call class OBJ.METHOD1 ("parameter "); // call method OBJ = null; // Release object // *********** CODE END *********** Note: a. Bind a function When you go to the method, you cannot add parameters and parentheses; b. Functions bound to methods can be external or internal (of course, in other files)

3, Property Sorry, JavaScript does not implement the effect as the attribute in the C #, the defined method and data member are public. However, we can use methods to access properties. // *********** CODE BEGIN *********** Function myclass () {var privatedata = 0; this. setdata = _func1; this.setdata = _func2;

// Get member function _func1 () {return privatedata;}

// Set the member function _func2 (value) {privateData = value;}} var obj = new myclass (); // call class Obj.SetData (100); Alert (obj.getdata ()); Obj = null; // Release object // *********** Code end *********** Note: a. Here is actually defined local variables within a function, unable access to the outside, so access Members' methods should also be defined inside the class.

4. In the Public & Private JavaScript, members can be accessed, as if the default is PUBLIC. How to achieve the private? Seeing 3 examples maybe it may be understood that the local variables can be privatable in the Class. The same is true of the method, as long as the function is not binding. // *********** CODE BEGIN *********** Function myclass () {var privatedata = 0; this. setdata = _func1; this.setdata = _func2;

// Get member function _func1 () {return privatedata;}

// Set the member function _func2 (value) {__privateMethod (); // Access private method, externally cannot access privateData = value;} function __privateMethod () {privateData = 1000; // Private method Access private member}} var Obj = new myclass (); // call class obj.setdata (100); obj.getdata (); obj = null; // Release object // *********** CODE END **** ****** Note: a. In public methods can access public methods and members, can also access private methods and members; however, private methods can only access private methods and members.

5. On the top section of the object itself (this), friends must find such a problem: How to access the object itself in a private method? If a function is not used. Method name = function name is bound, then use this in the function will generate an error. How to solve it? // *********** CODE BEGIN *********** Function myclass () {var me = this; // Note that the object itself will be referenced to another object. Var privatedata = 0; this.publicdata; this.getdata = _func1; this.setdata = _func2;

// Get member function _func1 () {return privatedata;}

// Set the member function _func2 (value) {this.publicData = 100; // Access a total data member __privateMethod (); // Access private method, externally cannot access privateData = value;}

Function __privateMethod () {me.publicData = 200; // Access a common method in private method private; // Access private member}} var obj = new myclass (); // call class Obj.SetData 100); obj.getdata (); obj = null; // release object // *********** Code end *********** Summary: When defining class To assign the object itself to a partial variable, the other functions can access the class by accessing this variable.

6. The function overload function overload can call different functions of the same name through different parameters. JavaScript can also be, just troublesome: // *********** code begin *********** Function func1 () // can be specified when defining functions, You can also not specify parameters {var Nargcount = func1.arguments.length; switch (nargcount) {case 0: // no parameters // code here break; case 1: // A parameter // code here break; ............ Default: // code here break;}} // ********** Code end *********** Note: a. Unfortunately, I haven't thought about how to according to parameters Method for judging the type.

7, constructor (constrcutor) Oh, because the class itself is a function, so writing code directly in the function is equivalent to the constructor. // *********** CODE BEGIN *********** Function myclass () {this.publicData; // The following code is equivalent to constructor this.publicData = 123; Alert (this.publicData);} var obj = new myclass (); // call class OBJ = null; // release object // ********** Code end ******* **** Note: The constructor does not use the parameters here, and can combine the sixth techniques to make the effect of constructor parameters. 8. Custom Event (Event) JavaScript, the function itself is also an object, can be used directly, so it is also very simple to define your own event. // *********** CODE BEGIN *********** Function myclass () {var privatedata = 0; this.SetData = _SETDATA; this.ondataachange = null;

Function _SetData (Value) {privateData = value; this.ondatachange (); // trigger event}} function myevent () {Alert ("You changed value!");} var obj = new myclass (); // call Obj.ondatachange = myEvent; obj.setdata (1234); obj = null; // Release object // *********** Code end ***********

9. Inherit using prototype to achieve inheritance: function myclass () {this.property = "value"; this.method = function () {}} var obj = new myclass (); obj.prototype.property1 = "value1"; obj .prototype.method1 = function () {} is ok, the above is the skills summed up, I hope to help everyone. At the same time, I will also initiate everyone: If multiple functions can be reused, or solve a problem can be reused, then the code of these JavaScript is written into a class. Good packaging, reusability!

******************************************** qq: 12304685 ** mail: ah_fu126 @ Hotmail. COM *****************************************

转载请注明原文地址:https://www.9cbs.com/read-12136.html

New Post(0)