前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >prototype.js中的class.create()方法

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

作者头像
全栈程序员站长
发布2022-09-15 10:29:15
5.2K0
发布2022-09-15 10:29:15
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

Class.create

代码语言:javascript
复制
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.cn/163401.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Class.create
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档