`
k1280000
  • 浏览: 203774 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

simulate classes in javascript

 
阅读更多

1. As is probably clear, you simulate a class property in JavaScript simply by defining a property of the constructor function itself to store a special 1x1 rectangle, you can do the following:

Rectangle.UNIT = new Rectangle(1,1); 

 


2. To define a class method in JavaScript, simply make the appropriate function a
property of the constructor.

// This class method takes two Circle objects and returns the

// one that has the larger radius.

       Circle.max = function(a,b) { if (a.r > b.r) return a; else return b; }
 

3. private members , you can refer to http://www.crockford.com/javascript/private.html for more info.

example :

function ImmutableRectangle(w, h) {

  // This constructor does not store the width and height properties

  // in the object it initializes. Instead, it simply defines // accessor methods in the object. These   //methods are closures and

// the width and height values are captured in their scope chains.

  this.getWidth = function( ) { return w; }

  this.getHeight = function( ) { return h; }

} 
 


4. JavaScript equality operators compare objects by reference, not by value.

5. Superclasses and Subclasses

// Here is a simple Rectangle class.
// It has a width and height and can compute its own area
function Rectangle(w, h) {
    this.width = w;
    this.height = h;
}
Rectangle.prototype.area = function( ) { return this.width * this.height; }

// Here is how we might subclass it
function PositionedRectangle(x, y, w, h) {
    // First, invoke the superclass constructor on the new object
    // so that it can initialize the width and height.
    // We use the call method so that we invoke the constructor as a
    // method of the object to be initialized.
    // This is called constructor chaining.
    Rectangle.call(this, w, h);

    // Now store the position of the upper-left corner of the rectangle
    this.x = x;
    this.y = y;
}

// If we use the default prototype object that is created when we
// define the PositionedRectangle( ) constructor, we get a subclass of Object.
// To subclass Rectangle, we must explicitly create our prototype object.
PositionedRectangle.prototype = new Rectangle( );

// We create this prototype object for inheritance purposes, but we
// don't actually want to inherit the width and height properties that
// each Rectangle object has, so delete them from the prototype.
delete PositionedRectangle.prototype.width;
delete PositionedRectangle.prototype.height;

// Since the prototype object was created with the Rectangle( ) constructor,
// it has a constructor property that refers to that constructor.  But
// we want PositionedRectangle objects to have a different constructor
// property, so we've got to reassign this default constructor property.
PositionedRectangle.prototype.constructor = PositionedRectangle;

// Now that we've configured the prototype object for our subclass,
// we can add instance methods to it.
PositionedRectangle.prototype.contains = function(x,y) {
    return (x > this.x && x < this.x + this.width &&
            y > this.y && y < this.y + this.height);
}
 



creating a subclass in JavaScript is not as simple as creating a class that
inherits directly from Object. First, there is the issue of invoking the
superclass constructor from the subclass constructor. Take care when you do this
that the superclass constructor is invoked as a method of the newly created
object. Next, there are the tricks required to set the prototype object of the
subclass constructor. You must explicitly create this prototype object as an
instance of the superclass, then explicitly set the constructor property of the
prototype object.[*]Optionally, you may also want to delete any properties that the superclass constructor created in the prototype object because what's important are the properties that the prototype object inherits from its prototype.
guess

print(r instanceof PositionedRectangle &amp;&amp;
      r instanceof Rectangle &amp;&amp;
      r instanceof Object);

 will return what ?

--------------------------------------------------------------

ps: what is prototype ?
In the previous section, I showed that the new
operator creates a new, empty object and then invokes a constructor function as a method of that object. This is not the complete story, however. After creating the empty object, New
sets the prototype of that object. The prototype of an object is the value of the prototype property of
its constructor function. All functions have a prototype property that is automatically created and initialized when the function is defined. The initial value of the prototype property is an object with a single property. This property is named constructor and refers back to the constructor function with which the prototype is associated. (You may recall the constructor property from Chapter 7 ; this is why every object has a constructor property.) Any properties you add to this prototype object will appear to be properties of objects initialized by the constructor.

 

 

Constructor Chaining

In the example just shown, the PositionedRectangle( ) constructor function needed to explicitly invoke the superclass constructor function. This is called constructor chaining and is quite common when creating subclasses. You can simplify the syntax for constructor chaining by adding a property named superclass to the prototype object of the subclass:

// Store a reference to our superclass constructor.
PositionedRectangle.prototype.superclass = Rectangle;
 

With this property defined, the syntax for constructor chaining is simpler:

function PositionedRectangle(x, y, w, h) {
    this.superclass(w,h);
    this.x = x;
    this.y = y;
}
 

 

Note that the superclass constructor function is explicitly invoked through the this object. This means that you no longer need to use call( ) or apply( ) to invoke the superclass constructor as a method of that object.

 

more about superclass

more

 

 

6. Extending Without Inheriting

 JavaScript is such a flexible language that subclassing and inheritance is not the only way to extend a class.

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    LiquidCrystal_in_arduino_and_simulate_in_p_java_arduino_matlab_p

    标题 "LiquidCrystal in arduino and simulate in proteus very easy" 提到的是在 Arduino 平台上使用 LiquidCrystal 库控制LCD显示屏,并通过 Proteus 进行仿真。这个项目旨在教你如何在实际硬件和模拟环境中实现...

    Process Simulate虚拟调试

    标题《Process Simulate虚拟调试》中提到的知识点是关于如何利用西门子数字化工厂Tecnomatix软件中的Process Simulate模块进行虚拟调试。Tecnomatix是西门子公司提供的一套用于数字化工厂规划和管理的软件解决方案,...

    simulate4.2卡密破解

    S95624633158 S51324610986 S45331655664 S23302819004 S37141895703 S69534899518 S74520514490 S39716109586 V:fff161

    Android-Simulate.rar

    "Android-Simulate"项目就是这样一个工具,它实现了在PC上运行Android应用,利用OpenGL ES 1.1来模拟图形渲染,为开发者提供了一个便捷的测试平台。 一、OpenGL ES 1.1简介 OpenGL ES是OpenGL的一个子集,专为...

    基于Process Simulate的机器人运动程序开发及应用.pdf

    基于Process Simulate的机器人运动程序开发及应用 基于Process Simulate的机器人运动程序开发及应用是当前汽车行业中的一种重要技术,旨在提高机器人离线编程效率和质量,大大减少真实环境调试的时间和成本。在此...

    simulate_v4.0_官方正版.zip

    《使用simulate_v4.0_官方正版.zip进行iOS设备系统定位调试详解》 在iOS设备的开发和测试过程中,系统定位功能的调试是一项至关重要的任务。传统的调试方式往往需要设备越狱,但这不仅存在安全风险,也会导致软件的...

    前端开源库-express-simulate-latency

    ```javascript const express = require('express'); const simulateLatency = require('express-simulate-latency'); const app = express(); // 在所有路由上应用默认延迟 app.use(simulateLatency(1000))...

    前端项目-jquery-simulate.zip

    【前端项目-jquery-simulate.zip】是一个包含前端开发中使用的jQuery插件,该插件主要功能是模拟浏览器中的鼠标和键盘事件。这个压缩包的核心是`jquery-simulate-master`目录,它很可能包含了源码、示例、文档等相关...

    Simulate ONTAP 8.0 Installation and Setup Guide

    Simulate ONTAP 8.0是Data ONTAP® 8.0软件的虚拟模拟器。Data ONTAP是NetApp公司的一款企业级存储操作系统,广泛用于其存储阵列产品中。ONTAP提供了一系列的数据管理功能,比如数据保护、存储虚拟化、数据复制等。...

    simulate demo

    在IT行业中,模拟(simulate)是一项重要的技术,它允许开发者在真实环境之外创建虚拟环境来测试和验证软件或系统的功能。"simulate demo"这个标题暗示我们可能会探讨一个用于演示模拟技术的实例或项目。这个项目...

    Process Simulate Basics.pdf

    根据提供的文件信息,“Process Simulate Basics.pdf”是一个关于Process Simulate软件的基础教程。这份文档包含了学生指南、版本信息以及版权声明等内容。接下来将基于这些信息详细展开相关知识点。 ### Process ...

    gx simulate6-7.rar

    gx simulate6-7rar,gx simulate6-7

    gx simulate6-4.rar

    gx simulate6-4rar,gx simulate6-4

    simulate.exe

    simulate.exe

    Simulate_v4.0.2.rar

    《Simulate_v4.0.2.rar》是一个压缩文件,其命名表明这可能是某个软件的模拟或仿真工具的版本4.0.2。"Simulate"通常与模拟、仿真实验或者虚拟环境有关,可能是一个用于测试、学习或开发目的的软件。版本号4.0.2暗示...

    simulate游戏的python实现

    应用于python3版本的simulate游戏,很多玩家对此爱不释手,但不知道如何用python语言实现它,该源程序可直接运行,运行后立即开始游戏,还在等什么!

    simulate queue length

    "simulate queue length"的程序通常用于预测和优化系统的性能,通过模拟真实世界的情景来理解服务系统如何处理输入负载。以下是关于这个主题的详细知识: 1. **排队论基础**:排队论是运筹学的一个分支,它研究服务...

    onenet虚拟设备调试工具sandbox_simulate-device2.2.3

    标题中的“onenet虚拟设备调试工具sandbox_simulate-device2.2.3”指的是中国移动OneNet平台提供的一个模拟物联网(IoT)设备的调试工具。这个工具的版本为2.2.3,它允许开发者在不实际操作物理硬件的情况下,仿真...

Global site tag (gtag.js) - Google Analytics