Learn JS so long time, or the object of the knowledge is not very understanding, recently took time to summarize, look at the object of JS in the world.
Creating a class in 1.JS
Creating classes in JS is especially easy, usually using function to declare
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><script> //Create class function people () { } //Determine the type of class var someone = new people (); Alert (someone instanceof people); </script></span>
Properties of a class in 2.JS
To create a class, let's look at how to customize the properties
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><script> //Class attribute function people (name, sex) { this.name = name; This.sex = sex; } var Susan = new People ("Susan", "female"); alert (susan.name); alert (susan.sex); </script></span>
Methods for classes in 3.JS
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><script> //class method function people (name, sex) { this.name = name; This.sex = sex; This.changename = function (newName) { this.name = newName; } } var someone = new people ("Susan", "female"); alert (susan.name); Someone.changename ("Lily"); alert (someone.name); </script></span>
Public and private properties of classes in 4.JS
Properties that are added through the this pointer in a class are public properties, and private properties are Var
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" > <!--Common and private properties--<!--the properties that are added through the this pointer in a class are public properties. Public properties are properties that can be accessed externally--<script> function people (ndame, sex, deposit) {this.name = name; This.sex = sex; var deposit = deposit; This.changename = function (newName) {this.name = NewName; } This.consume = function (Money) {if (deposit >= money) {Deposit-= Mon ey } else {throw new Error ("There is not enough deposit"); }}} var Susan = new People ("Susan", "female", 1000); var name = Susan.name; var deposit = Susan.deposit; No access, private attribute alert (deposit); try {susan.consume (500); } catch (e) {alert (e.message); } try {susan.consume (1000); } catch (e) {alert (e.message); } </script≫</span>
5. Public methods and private methods
Attributes are public and private, and so are the methods. Similar concepts.
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" > <!--Public and private methods--<script> function people (name, sex, deposit) {this.name = name; This.sex = sex; var deposit = deposit; Private attribute This.thew = 1; Public attribute This.changename = function (newName) {this.name = NewName; } This.consume = function (Money) {this.consume = function (Money) {if (DEP Osit >= Money) {Deposit-=-money; } else {throw new Error ("There is not enough deposit"); }}} var _this = this;//Saves the current object to a variable, _this for private methods using var digest = function (fo OD) {//Private method digest _this.thew++; Within the anonymous function, the this pointer points to a change, so the program uses _this to save the People object reference} This.eat = function (food) {//Public method eat Digest (food); }} </scripT></span>
6. Static Properties and methods
There are also static properties and methods in object-oriented to see how JS is done
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" > <!--static properties and Static methods --<!--constructors themselves are objects that add properties and methods directly to this object, you can achieve the effects of static and static methods-- <script > Function people () {} People.staticproperty = "static property"; static property People.staticmethod = function () {} //static method </script></span>
7. Prototype objects
The prototype object is like a backup of an object definition, and if it does not exist in the object reference when the code refers to the property, it will be automatically found in the Love prototype
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" > <!--prototype objects--<!--prototype object is like a backup of an object definition, and when the code references a property, if it doesn't exist in the object reference, it will automatically look for this attribute in the love archetype-<script> function people (name, sex, deposit) {this.name = name; This.sex = sex; var deposit = deposit; private Property This.consume = function (Money) {this.consume = function (Money) { if (deposit >= money) {deposit-= money; } else {throw new Error ("There is not enough deposit"); }}} var _this = this;//Saves the current object to a variable, _this for private methods using var digest = function (fo OD) {//Private method digest _this.thew++; Within the anonymous function, the this pointer points to a change, so the program uses _this to save the People object reference} This.eat = function (food) {//Public method eat Digest (food); }} people.prototype = {thew:1, Changename:function (newName) {this.name = NewName; }} </script></span>
Object-oriented in JavaScript