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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【杀软】Win7内置恶意软件删除工具——MRT

    【杀软】Win7内置恶意软件删除工具——MRTWindows7的WindowsDefender是一款具备实时监控能力的专业恶意软件清除工具。可能许多人将其禁用了,换用其他具备恶意软件清除能力的世界顶级杀软了(比如说笔者就用的NOD32,对恶意软件查杀能力甚是强悍)。如果你不忍WindowsDefender占用大量内存,又不舍得丢弃微软提供的服务,如何是好?有一个折中的方案:使用与其具有相同资源库的MRT…

    2022年6月24日
    88
  • nessus安装教程_kali安装nessus

    nessus安装教程_kali安装nessus(Windows安装教程)step1.官网下载对应操作系统安装包https://www.tenable.com/downloads/nessus1.pngstep2.正常双击程序安装等到最后一步finish完成后等会自动跳转出一个注册页面,或者打开浏览器输入https://localh…

    2022年10月10日
    1
  • pycharm2018激活码 pycharm激活码_软件一键无痕看怎么使用的

    pycharm2018激活码 pycharm激活码_软件一键无痕看怎么使用的PyCharm激活码最新破解教程,Mac版激活至2299年,PyCharm激活码2021.3.3

    2022年4月20日
    157
  • Mac OS 查看 ip 地址及 DHCP 各 addr 含义「建议收藏」

    Mac OS 查看 ip 地址及 DHCP 各 addr 含义

    2022年2月11日
    63
  • 如何有效解决高清视频传输中出现的闪屏、黑屏、蓝屏问题?

    如何有效解决高清视频传输中出现的闪屏、黑屏、蓝屏问题?春节期间冬奥会上女足夺冠的精彩赛事是不是还历历在目?你知道体育赛事、音乐会直播以及远程医疗等对视频清晰度和稳定性要求极高的传输是怎么实现的吗?为什么别人4K超高清的视频看起来如此流畅,而你一个1080P的高清视频却经常出现闪屏、黑屏、蓝屏?​在很多视频监控传输的项目,当我们做完项目后,经常会遇到闪屏、黑屏、蓝屏的问题,反复检查线路和接线后仍一筹莫展,查不出问题所在。这是为什么,工程师为您解答,如何有效解决高清视频传输中出现的闪屏、黑屏、蓝屏的问题。​我们都知道一般高清.

    2022年10月3日
    1
  • linux删除历史操作命令「建议收藏」

    linux删除历史操作命令「建议收藏」 在linux系统里一旦操作了任何命令,都会被记录下来,可以通过history命令来查看历史命令,  查看手册可知history-c便是清除历史命令,但是重新进入系统查看历史命令时,历史信息依然会存在,  那么history-c只是清除当前shell的历史纪录,因为系统一般会把信息保存在一个文件中,只要文件中  内容没有改变,那么信息也不会变。linux中存放历史命令的文件是….

    2022年6月22日
    29

发表回复

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

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