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解析db文件

    java数据库系统开发案例精选_Java解析db文件JAVADB数据库范例关于javaDB的介绍就不说了,直接上例子。首先将derby.jar添加到classpath下面(IDE的话直接引入)importjava.sql.*;importjava.util.*;importjava.io.*;publicclassTest{publicstaticvoidmain(String[]args)throwsException{DB…

    2025年7月31日
    2
  • ICMP报文分析

    ICMP报文分析

    2021年12月3日
    41
  • 验证码Kaptcha的使用「建议收藏」

    验证码Kaptcha的使用「建议收藏」Kaptcha是一个非常实用的验证码生成工具,可以通过配置生成多样化的验证码。以图片的形式显示,从而无法进行复制粘贴。

    2022年6月18日
    40
  • EasyDSS流媒体服务器软件-正式环境安装部署攻略

    EasyDSS流媒体服务器软件-正式环境安装部署攻略EasyDSS流媒体服务器软件,提供一站式的转码、点播、直播、时移回放服务,极大地简化了开发和集成的工作。其中,点播功能主要包含:上传、转码、分发。直播功能主要包含:直播、录像,直播支持RTMP输入,RTMP/HLS/HTTP-FLV的分发输出;录像支持自定义保存时长、检索及下载。提供丰富的二次开发接口,基于JSON的封装及HTTP调用。提供播放鉴权、推流鉴权等安全保证。提供用户及相关权限管理…

    2022年6月9日
    32
  • 数字电路实验(一)——译码器

    数字电路实验(一)——译码器1、实验步骤:异或门过程1、 新建,编写源代码。(1).选择保存项和芯片类型:【File】-【newprojectwizard】-【next】(设置文件路径+设置projectname为【C:\Users\lenovo\Desktop\笔记\大二上\数字电路\实验课\实验一\异或门】)-【next】(设置文件名【gg】)-【next】(设置芯片类型为【cyclone-EP1CT144C…

    2022年7月12日
    18
  • python是什么?python能做什么?

    python是什么?python能做什么?人生苦短,我用python。python是什么?Python是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。python语言有以下特点:易于学习。Python有相对较少的关键字

    2022年7月6日
    26

发表回复

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

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