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!"

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163401.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

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