prototype.js中的class.create()方法

prototype.js中的class.create()方法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

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 26var 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!"
编程小号
上一篇 2025-03-08 14:46
下一篇 2025-03-17 10:01

相关推荐

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