1.js Object-Oriented object
1.1JS Code
<Script>functionHello () {alert ("hi!");}//to add properties and methods to a class by using the prototype propertyHello.prototype={name:"Lihua", say:function() {alert ( This. Name); }} //TestfunctionTest () {varpeople=Newhello (); People.say (); alert (people.name);} Test (); //hi! Lihua Lihua</Script>
To add instance methods and properties to an object, we can use the prototype keyword to assign a value, in the following form:
The object name. prototype = {
Attribute one: Property value ,
Attribute two: Property value ,
Method One: function(argument list) {
Method body;
},
Method Two: function(parameter list) {
Method body;
}
}
You can define multiple properties and methods on an object as described above, so that you can use the instance name after you have a new object . Property or method to get the property or execution method.
1.2 The properties and methods in the preceding code are similar to Java has properties and methods, how to declare private properties and methods?
Functions in JavaScript allow nesting, and we can declare private methods and properties with inline functions and properties
JS Code
<Script>//using JavaScript to allow function nesting mechanisms to implement classesfunctionHello () {varPrivatepro="Hi"; This. Say=function() {alert (Privatepro); } }//TestfunctionTest () {varpeople=Newhello (); People.say (); //call the Say () method to print Hialert (people.privatepro);//Print undefined}test (); //hi! Undefined</Script>
1.3 What is described above is an instance method and a property that defines a class. In Java, classes have instances of methods and properties and class methods and properties. The so-called Class attribute and method is that all instances of the class maintain only one copy of the class properties and class methods, not each instance maintains a set, which is not the same as instance properties and instance methods. So how do you define a static class method and Class property for a class in JS?
Add static properties and static methods directly to a class
Note: Static properties and static methods can only be accessed through the class name and cannot be accessed through an instance
JS Code
<Script>//using JavaScript to allow function nesting mechanisms to implement classesfunctionHello () {varPrivatepro="Hi"; This. Say=function() {alert (Privatepro); } }//adding static properties and methods to the Hello classHello.age= at; Hello.getage=function() {alert (hello.age)}//TestfunctionTest () {varpeople=Newhello (); People.say (); //correct, instance method, call say () method by instance to print Hialert (people.privatepro);//Print undefinedHello.getage (); //correct, static methods are accessed through the class namePeople.getage (); //error, cannot access static method through instance, return JS error}test (); </Script>
2.JS Object-oriented inheritance
2.1js inheritance is broadly divided into two types: Object impersonation, prototype mode
JS Object-Oriented programming