JavaScript uses JavaScript to mimic OOP programming

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.