这章内容我前后看了三次,感觉现在还没有完全吃透。而且让我感觉到Javascript的难度不次于C++,当然,这最主要的原因是:
Javascript和C++/Java不一样,它不是基于Class的OO语言,而是基于Prototype的OO。这个要求我们转变思维对待它,不过说来容易,做起来太难。(我蛮期待Javascript能够引入Class的概念,注意是这样的理念,不是这个关键字)。
为什么要这样呢,因为实际上现在Class的OO是主流,几乎所有围绕OO的设计编码方法都是围绕这个展开的(我知道很多人可能要跳出来说什么语言不是决定,关键是思想),我也很清楚Javascript可以模拟很多Class的OO,但是我还是强烈的希望直接支持它。
1. 定义一个Class
function MyClass(v) {
this.value = v;
}
这么三行语句,简单说它就是定义了一个函数,其它什么都不是。这个函数的名字是MyClass,参数是v。函数体是:
this.value = v; 就是这么多,多一点都没有。可是当这些东西和下面这个new放在一起的时候,奇迹发生了。
var mc = new MyClass(10);
奇迹是什么呢?
[1] 你可以访问到mc.value,并且结果会返回10;
[2] 你如果用 mc instanceof MyClass会返回true
[3] mc.constructor == MyClass也会返回true。
2. 定义方法,类方法(静态方法),类属性(静态属性)
所有的方法,或者说对象的方法,都是定义在prototype里的。而类方法和类属性都是直接定义成函数的属性的,如下:
MyClass.prototype.toString = function() {
return "[MyClass:" + this.value + "]";
}
MyClass.count = 0;
MyClass.max = function(a,b) {
if (a.value > b.value) {
return a;
} else {
return b;
}
}
3. 继承
继承的几个关键性质:
[1] 如果C继承自P,那么var c = new C(); c instanceof P应该是true;
[2] 如果C继承自P,那么如果C没有定义函数foo,但是P里面定义了,那么C应该默认有这个函数
[3] Override的功能。
<html>
<head>
<title>
</title>
</head>
<body>
<script language="javascript">
function Person(aName, aAge, aSex) {
this.name = aName;
this.age = aAge;
this.sex = aSex;
}
Person.prototype.toString = function() {
return "[Person " + this.name + " " + this.age
+ " " + this.sex +"]";
}
function Employee(aName, aAge, aSex, aID) {
this.superclass(aName, aAge, aSex);
this.id = aID;
}
Employee.prototype = new Person();
delete Employee.prototype.name;
delete Employee.prototype.age;
delete Employee.prototype.sex;
Employee.prototype.superclass = Person;
Employee.prototype.toString = function() {
return "[Employee " + this.id + " " + this.superclass.prototype.toString.call(this);
}
var p = new Person("AnkyHe", 28, "Male");
document.write(p + "<br>");
var e = new Employee("AnkyHe", 28, "Male", "90891234");
document.write(e + "<br>");
document.write("e instance of Person " + (e instanceof Person) + "<br>");
</script>
</body>
</html>
第22行代码就是为了实现要求[1][2](Employee.prototype = new Person(); )本质上就是为了使得Employee.prototype成为一个Person,然后所有定义在Person.prototype的方法(成员方法)都可以通过Employee.prototype访问到,也就是说成为Employee的成员方法。Override toString的时候,注意直接调用父类的方法。要用到apply或者call这种方法^_^
4. Mixni
这不是第一次听说这个概念了,最早是在Ruby里面。不过Ruby对Minxi的支持要强大一些,这个里面还要自己去定义一个borrow这样的函数。
<html>
<head>
<title>
</title>
</head>
<body>
<script language="javascript">
function borrow(from, to) {
for (var name in from.prototype) {
if (typeof from.prototype[name] != "function") {
continue;
}
to.prototype[name] = from.prototype[name];
}
}
function MixinToString() {}
MixinToString.prototype.toString = function() {
var rst = []
for (var name in this) {
if (!this.hasOwnProperty(name)) {
continue;
}
var value = this[name];
var s = name + ":";
switch(typeof value) {
case "function": s += "function"; break;
case "object":
{
if (value instanceof Array) {
s += "array";
} else {
s += value.toString();
}
break;
}
default: s += value.toString();
break;
}
rst.push(s);
}
return "{" + rst.join(", ") + "}";
}
function Book(aTitle, aAuthor, aPrice) {
this.title = aTitle;
this.author = aAuthor;
this.price = aPrice;
}
borrow(MixinToString, Book);
var book = new Book("Begin iPhone", "AnkyHe", 10);
document.write(book + "<br>");
</script>
</body>
</html>
分享到:
相关推荐
Vcl.SuperObject 是 Delphi 开发环境中一个用于处理 JSON 数据的库,其核心类为 SuperObject_class。这个库提供了一种高效且灵活的方式来创建、解析和操作 JSON 对象。在 Delphi 中,SuperObject 使得与 JSON 格式的...
Static data members are common to all objects of a class, and their value is not unique per object. Static member functions, also known as class methods, can be called without creating an instance of...
existing superpixel classication framework for semantic scene segmenta- tion and achieve a 24% relative improvement over current state-of-the-art for the object categories that we study.We believe ...
Systems Analysis and Design: An Object-Oriented Approach with UML, 5th Edition by Dennis, Wixom, and Tegarden captures the dynamic aspects of the field by keeping students focused on doing SAD while ...
the full 3D shape and pose of all object instances in the image. Our method produces a compact 3D representation of the scene, which can be readily used for applications like autonomous driving. Many ...
Object-Oriented Analysis and Design for Information Systems illustrates how and why building a class model is not just placing classes into a diagram. You will learn the necessary organizational ...
Object Oriented Analysis and Design 12Singleton - MotivationA Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. This is beneficial ...
面向对象分析与设计(Object-Oriented Analysis and Design,简称OOAD)是软件开发过程中关键的两个阶段,它涉及到对问题域的深入理解以及创建一个能够反映这些理解的模型。本章节主要讨论了统一建模语言(Unified ...
Part I provides a foundation for real-time C++ by covering language technologies, including object-oriented methods, template programming and optimization. Next, part II presents detailed ...
#### 一、面向对象分析与设计(Object-Oriented Analysis and Design, OOAD) 面向对象分析与设计(Object-Oriented Analysis and Design, OOAD)是一种软件工程方法论,用于在软件开发过程中对软件系统进行建模。...
Object Pascal Handbook This is the book summary Table of Contents: Part I Chapter 1: Coding in Pascal Chapter 2: Variables and Data Types ...Chapter 17: The TObject Class Chapter 18: RunTime Library
比如,可能会定义“对象类”(Object Class)是标识一组具有相同属性的实体的类别,而“属性类型”(Attribute Type)则是用于描述对象类成员特征的标识符。 - **DEFINITIONS**:这部分列出并解释了关键术语,确保...
【解惑】类与对象的初始化问题 - 爪哇人
C++程序设计教学课件:Chapter 3 class and object 本节课程主要介绍C++程序设计中的类和对象,涵盖了面向对象编程、类和对象的定义、构造函数和析构函数、组合、静态成员、常量对象和常量成员函数等关键概念。 一...
在提供的资源中,"How to Create an Object Class in MATLAB.mp4"可能是一个视频教程,详细展示了如何实际操作创建对象类的过程,而"How to Create an Object Class in MATLAB - 韩语 中文(简体)(双语).srt"则可能...
面向对象编程(Object-Oriented Programming,简称OOP)是一种编程范式,它将程序设计围绕“对象”这一核心概念来构建。对象是数据和可以作用于这些数据上的函数的封装体。在OOP中,通过定义类来创建对象,而类则是...
2.6 BIBLIOGRAPHICAL NOTES AND OBJECT RESOURCES 34 PART B: THE ROAD TO OBJECT ORIENTATION 37 Chapter 3: Modularity 39 3.1 FIVE CRITERIA 40 3.2 FIVE RULES 46 3.3 FIVE PRINCIPLES 53 3.4 KEY CONCEPTS ...