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


相关推荐

  • django修改数据_django-vue-admin

    django修改数据_django-vue-admin前言在ORM框架中,所有模型相关的操作,比如添加/删除等。其实都是映射到数据库中一条数据的操作。因此模型操作也就是数据库表中数据的操作。添加一个模型到数据库中:添加模型到数据库中。首先需要创建一

    2022年7月28日
    3
  • 2021年十大开源web应用防火墙

    2021年十大开源web应用防火墙开源web应用防火墙是网络安全的重要部分,Cloudflare认为:十年后数字经济的网络安全基础设施会像水过滤系统一样普及,而这个过滤系统的核心就是waf。对于服务器来说,部署WEB应用防火墙十分重要,笔者经过大量搜索,并结合市场热度,整理出2021年十大开源web应用防火墙。1、OpenRestyOpenResty是由中国人章亦春发起,把nginx和各种三方模块的一个打包而成的软件平台,核心就是nginx+lua脚本语言。主要是因为nginx是C语言编写,修改很复杂,而lua语言则简单得多,国内很多

    2022年6月2日
    193
  • QQ邮箱开启SMTP服务的步骤

    QQ邮箱开启SMTP服务的步骤

    2021年9月21日
    95
  • python3.6.0-32 sqlite tkdnd tkinterdnd2 拖拽 快捷方式管理

    python3.6.0-32 sqlite tkdnd tkinterdnd2 拖拽 快捷方式管理

    2021年6月8日
    138
  • pycharm上如何拉git代码_pycharm版本控制

    pycharm上如何拉git代码_pycharm版本控制pycharm设置git与deployment1、创建project2、vcs创建git目录,指向project目录3、添加git远程地址4、拉取对应分支代码即可5、设置deployment5.1设置SFTP5.2配置信息5.3设置远程服务器文件地址

    2022年8月28日
    4
  • 云服务器怎么配置cpu与内存搭配「建议收藏」

    云服务器怎么配置cpu与内存搭配「建议收藏」很多朋友在购买云服务器之前都会搜服务器一般用几核才够用,因为服务器现在配置很多。低到1核2G、2核4G。高到16核32G、32核64G。甚至某些云服务器可以做到256核5120G这种神奇配置。那么购买云服务器时如何选择cpu与内存搭配?出现资源不足时应如何排查原因呢?一、处理器性能解析首先要明确一点,虽然都是多少核。但是服务器的处理器性能还是有差异的。具体可以搜对应处理器CPU性能天梯。阿里云的服务器都是定制…

    2022年5月22日
    408

发表回复

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

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