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


相关推荐

  • Netty学习之读netty权威指南(一)

    Netty学习之读netty权威指南(一)大家问我为什么读这个来学netty,嗯嗯嗯??我也说不上来,因为我以前看过某个培训班的课程,初步了解了一下netty,但是现在回想一下发现我所有的知识基本忘光了,不过没关系,慢慢来,一点一点的找回来不久好了吗,现在开始咱们读一读Netty权威指南这本书,学习一下Netty。当然了不会全部按照这本书来,我会加上自己学习的东西。I/O演进之路JDK1.4以前Java对IO的支持不完…

    2022年10月2日
    1
  • 论坛提问智慧

    论坛提问智慧本文转载自http://bbs.weblogicfans.net/thread-3628-1-1.html.一、确定你自己无法解决该问题 首先你至少应该解决问题花费1个小时以上的时间,并最终确定自己无力解决。问题的解决并不能够完全依靠他人。二、查看论坛精华文章 你遇到的问题很有可能已经有文章详细的论述如何解决了,精华文章往往是大家最需要留心关注的地方。 三、使用

    2022年7月26日
    5
  • 动画插件–AnimateCSS

    动画插件–AnimateCSS1.什么是Animate.css?其实swiper-animate就是参考Animate.css演变出来的一个插件, Animate.css和swiper-animate一样都是用于快速添加动画的, 所以会用swiper-animate就会用Animate.css2.Animate.css的使用:引入animate.css的文件 给需要执行动画的元素添加类名3.示例animated这个类名是animated.css的基类,但凡需要通过animated.css来添加动画,都需

    2022年7月27日
    15
  • 操作系统(第四版)期末复习总结(中)

    操作系统(第四版)期末复习总结(中)衔接我的上一篇博文,这片从第三章开始第三章:处理机调度与死锁

    2022年10月19日
    2
  • linux vlc乱码,一劳永逸解决VLC播放中文字幕乱码问题

    linux vlc乱码,一劳永逸解决VLC播放中文字幕乱码问题VLC对于Mac/Ubuntu用户来说算得上是必备软件。其相当于PC机上的“暴风影音”,但Mac/Ubuntu的新手使用VLC播放avi时都会碰到字幕乱码的问题。avi字幕的格式有多种,这里假设你使用常见的.srt字幕。VLC默认支持的字幕内码为utf-8,而网上提供的.srt字幕基本上都是GBK码,所以在初装VLC后的默认状态下,加载.srt字幕都会出现乱码。本教程以当前最新的VLC1.1…

    2022年7月11日
    37
  • nginx一个端口配置多个项目_映射地址怎么设置

    nginx一个端口配置多个项目_映射地址怎么设置Nginx默认的80端口如果想要同时配置多个项目,让项目实现不需要指定端口号即可访问,按照如下配置即可更多精彩更多技术博客,请移步IT人才终生实训与职业进阶平台-实训在线前置内容使用Nginx部署Vue项目这片笔记里面介绍了如何使用Nginx部署项目找到对应项目的Nginx配置一般比较规范的配置方式是为每个单独的项目创建.conf文件,如…

    2025年9月3日
    15

发表回复

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

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