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)
上一篇 2022年7月22日 下午11:36
下一篇 2022年7月22日 下午11:36


相关推荐

  • OpenClaw小龙虾在企业为何失灵?聊聊组织级AI智能体的三道墙

    OpenClaw小龙虾在企业为何失灵?聊聊组织级AI智能体的三道墙

    2026年3月13日
    3
  • 安利一款超级无敌好用的pycharm字体 Fira Code

    安利一款超级无敌好用的pycharm字体 Fira CodeFiraCodeFira 是 Mozilla 主推的字体系列 FiraCode 是基于 FiraMono 等宽字体的一个扩展 主要特点是加入了编程连字特性 ligatures FiraCode 就是利用这个特性对编程中的常用符号进行优化 比如把输入的 直接显示成 或者把 gt 变成 等等 以此来提高代码的可读性 安装方法首先点击下载字体包密码

    2026年3月17日
    2
  • C++中关键字volatile和mutable用法

    C++中关键字volatile和mutable用法C C 中的 volatile 关键字和 const 对应 用来修饰变量 用于告诉编译器该变量值是不稳定的 可能被更改 使用 volatile 注意事项 1 编译器会对带有 volatile 关键字的变量禁用优化 Avolatilespe

    2026年3月18日
    1
  • C语言面试54题

    C语言面试54题C 语言面试 54 题 大家好 这期呢 我们谈一下 c 语言的面试题 第 1 题 c 语言有哪些核心的特征 可移植性很强 模块化能力很强 灵活性很高 加载速度和执行速度都很好 可扩展性很强 第 2 题 c 语言中有哪些基本的数据类型 Int 整型 Float 浮点型 Double 双浮点型 Char 单个字符 void 特殊类型 不包含任何值 第 3 题 解释一下语义错误 在写程序的时候会有很多语义错误 比如说 拼错了命令 一个函数的参数个数错了 数据类型不匹配 等等 第 4 题 C 语言中如何使用增加和减少

    2026年3月18日
    2
  • 互联网公司程序员和外包公司程序员有什么区别?

    互联网公司程序员和外包公司程序员有什么区别?互联网的到来就注定会有外包公司的诞生,起初外包公司给一些不愿意花高代价招程序员的创业型小企业做独立外包,后来渐渐的大型的互联网公司开始出现,他们愿意把一些自己不熟悉或者繁琐的的领域和功能模块外包给专业能力更强的外包团队。从本质上讲,互联网公司和外包公司都是以盈利为己任。但…

    2026年4月16日
    5
  • OpenClaw小龙虾软件原理

    OpenClaw小龙虾软件原理

    2026年3月16日
    2

发表回复

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

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