First
First, define a class using the keyword function
function Shape1 (ax,ay) {// The function is considered to be the flag of the declaring class var x=0 ; var y=0 ; var init=function () {// The constructor assigns a value to the internal variable x=AX; Y =ay; }init (); // constructor call this . Getx=function () {// this declaration public Function Var declaration private get method return X; }}
Then, the instantiation of the object + call
var shape=New Shape1 (1,2); // Instantiate alert (Shape.getx ()); // invocation of public methods
Second
static properties and Static methods
The static method in JS is on the class rather than on the object
One, for classes that use a function declaration
(1) First, a class is defined first
function person () {this. Name=" Xiao Li "};
(2) Then, add static variables to the class static method
person.age=0; Person.showname=function (obj) { console.log (obj). Name)//The name at this time is the global variable under the Person object class, which requires the person object to be accessible };
(3) Call
Person.showname (new person ());
Summary: First class, then add static members,
The person is a class that can be instantiated, and the following static members need to be instantiated. To access
Second, a class (object) without a function declaration--Simple class
(1), First a class
var a={}; // a class
(2) Adding properties to a class
a["name"]="1"; // Add Property
(3) Call
alert (a.name);
JavaScript uses JavaScript to mimic OOP programming