prototype.js中的class.create()方法

prototype.js中的class.create()方法Class.createClass.create([superclass][,methods…])→Classsuperclass (Class) –Theoptionalsuperclasstoinheritmethodsfrom.methods (Object) –Anobjectwhosepropertiesw

大家好,又见面了,我是你们的朋友全栈君。

Class.create

Class.create([superclass][, methods...]) → Class
  • superclass (Class) – The optional superclass to inherit methods from.
  • methods (Object) – An object whose properties will be “mixed-in” to the new class. Any number of mixins can be added; later mixins take precedence.

Class.create creates a class and returns a constructor function for instances of the class. Calling the constructor function (typically as part of a new statement) will invoke the class’s initialize method.

Class.create accepts two kinds of arguments. If the first argument is a Class, it’s used as the new class’s superclass, and all its methods are inherited. Otherwise, any arguments passed are treated as objects, and their methods are copied over (“mixed in”) as instance methods of the new class. In cases of method name overlap, later arguments take precedence over earlier arguments.

If a subclass overrides an instance method declared in a superclass, the subclass’s method can still access the original method. To do so, declare the subclass’s method as normal, but insert $super as the first argument. This makes $super available as a method for use within the function.

To extend a class after it has been defined, use Class#addMethods.

For details, see the inheritance tutorial on the Prototype website.

链接:http://www.prototypejs.org/api

小demo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
var 
Animal = Class.create();
Animal.prototype = {
  
initialize: 
function
(name, sound) {
    
this
.name  = name;
    
this
.sound = sound;
  
},
  
speak: 
function
() {
    
alert(name + 
" says: " 
+ sound + 
"!"
);
  
}
};
var 
snake = 
new 
Animal(
"Ringneck"

"hissssssssss"
);
snake.speak();
// -> alerts "Ringneck says: hissssssssss!"
var 
Dog = Class.create();
Dog.prototype = Object.extend(
new 
Animal(), {
  
initialize: 
function
(name) {
    
this
.name  = name;
    
this
.sound = 
"woof"
;
  
}  
});
 
 
var 
fido = 
new 
Dog(
"Fido"
);
fido.speak();
 
// -> alerts "Fido says: woof!"

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163401.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • matlab 二分法区间,多区间二分法[通俗易懂]

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp预备知识 二分法这里介绍一种多区间二分法,可以求出连续函数在某区间内几乎全部的根.方法就是把这个区间等分为若干个相等的小区间,然后分别判断这些小区间两端函数值的符号,对所有两端异号的区间使用二分法即可.显然,小区间的个数越多,越有可能找到所有的根.例程如下.代码1:bis…

    2022年4月11日
    126
  • java: integer number is too large「建议收藏」

    java: integer number is too large「建议收藏」今天想定义一个类常量,结果如下面那样定义,确报错了。erroris:IntegernumbertoolargepublicstaticfinalLongSTARTTIME=1493568000000;12我就去查了下,在后面加上L,就好了,就会作为long类型来处理了,若是不加,则作…

    2022年7月17日
    52
  • ORB-SLAM2代码详解01: ORB-SLAM2代码运行流程

    ORB-SLAM2代码详解01: ORB-SLAM2代码运行流程ORB SLAM2 代码详解 01 ORB SLAM2 代码运行流程运行官方 Demo 阅读代码之前你应该知道的事情变量命名规则理解多线程为什么要使用多线程 多线程中的锁 SLAM 主类 System 构造函数跟踪函数运行官方 Demo 以 TUM 数据集为例 运行 Demo 的命令 Examples RGB D rgbd tumVocabular ORBvoc txtExamples RGB D TUM1 yamlPATH TO SEQUENCE FOLDERASSOCI FILErgbd tu

    2025年9月8日
    3
  • 可靠性测试的基础知识——可靠性的计算方法

    可靠性测试的基础知识——可靠性的计算方法计算机系统的可靠性从它开始运行(t=0)到某时刻t这段时间内能正常运行的概率,用R(t)表示。失效率指单位时间内失效的元件数与元件总数的比例,以λ表示,当λ为常数时,可靠性与失效率的关系为:R

    2022年7月4日
    27
  • 磁力链接文件服务器,什么是磁力链接(BT、磁力链这些词语是什么意思?)

    磁力链接文件服务器,什么是磁力链接(BT、磁力链这些词语是什么意思?)“知其然知其所以然”。我们经常在下载资料的时候能看到BT、磁力链等词语,百思特网这些词语到底是什么意思呢?下载都会用,但是你了解吗?BT下载传统的下载模式是每个客户端从服务器拷贝文件,跟校园内常用的FTP一样。因为服务器宽带是一定的,所以下载的人越多下载速度会越慢。而现在使用的下载器情况正好相反,使用的人越多文件下载速度越快。这是因为现在的下载器普遍采用类似BT的下载方式。布拉姆科恩发明了BT协议…

    2022年8月10日
    13
  • shell输出数组元素_shell中使用数组

    shell输出数组元素_shell中使用数组数组介绍平时的定义a=1,b=2,c=3,变量如果多了,再一个一个定义很费劲,并且取变量的也费劲简单的说,数组就是相同数据类型的元素按一定顺序排列的集合数组就是把有限个类型相同的变量用一个名字命名,然后用编号区分他们得边合。这个名字成为数组名,编号成为数组下标。组成数组的各个变量成为数组的分称为数组的元素,有时也称为下标变量数组定义与增删改查法1:array=(value1value2valu…

    2025年7月23日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号