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


相关推荐

  • java补码运算_java中的补码运算

    java补码运算_java中的补码运算publicclassTest2_8{/*补码运算*在计算机中,数值一率采用补码来运算,如:5-3实例上是5+(-3);*正数与负数的关系:取反再加1**/publicstaticvoidmain(Stringargs[]){intfive=5;intthree=-3;//从输出结果来看负数是用补码来存储的//输出5和-3的二进制码,最高位(最左边那位)为0表示正数,为1…

    2022年9月22日
    0
  • B – Fedya and Maths 暴力找规律入门

    B – Fedya and Maths 暴力找规律入门

    2021年9月28日
    37
  • js中settimeout()的用法详解_低噪放工作原理

    js中settimeout()的用法详解_低噪放工作原理基本原理setTimeout(func,delay,args):设置超时调用,经过delay时间后,将func函数加入到执行队列中准备调用。如果队列为空,立即执行该函数,否则等待线程空闲再执行。setInterval(func,interval,args):设置…

    2022年10月4日
    0
  • android 8原生系统下载地址,android8.0下载地址 android8.0系统下载网址[通俗易懂]

    android 8原生系统下载地址,android8.0下载地址 android8.0系统下载网址[通俗易懂]android8.0系统下载网址:大家期待的android8.0,命名为androido终于正式公布了,对于大家最关心的android8.0发布时间和新特性方面,谷歌表示今年夏季末,用户就可以升级了。本次开发者大会上,谷歌并没有公布android8.0的新功能,围绕新系统只有两点FluidExperience(流畅体验)和Vitals(核心功能)。简单来说就是,FluidExperience的内容…

    2022年6月19日
    52
  • 解决启动IIS发生意外错误 0x8ffe2740「建议收藏」

    解决启动IIS发生意外错误 0x8ffe2740「建议收藏」有时候也不知怎么搞的,你会突然间发现你的IIS启动不了了,提示“发生意外错误0x8ffe2740”这样的东东主要原因是80端口被程序占用了,请确认一下有没有程序占用了80端口,不知道怎么确认?可以用TcpView查看占用的端口然后终止这人端口其实大多情况下都是因为Xunlei或者数据库(SqlServer,Oracle)占用了,所以先把X…

    2022年7月26日
    3
  • Flume学习笔记「建议收藏」

    Flume学习笔记「建议收藏」Flume学习笔记Flume定义Flume基础架构Flume安装部署监控端口数据实时监控单个追加文件实时监控目录下多个新文件Flume定义Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集、聚合和传输的系统。Flume基于流式架构,灵活简单.Flume最主要的作用就是:实时读取服务器本地磁盘的数据,将数据写入到HDFS.Flume基础架构AgentAgent是一个JVM进程,它以事件的形式将数据从源头送至目的。Agent主要有3个部

    2025年6月2日
    0

发表回复

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

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