7.在 JavaScript 中扩展 mxGraph(javascript扩展代码)
yund56 2025-07-17 00:03 14 浏览
2.2.1.3 Extending mxGraph in JavaScript/ 在 JavaScript 中扩展 mxGraph
[翻译]
在 JavaScript 中,有多种方式将面向对象范式映射到语言结构。mxGraph 在整个项目中使用了一种特定的方案,遵循以下隐式规则:
- 不更改内置原型。
- 不尝试限制 JavaScript 语言的功能。
[原文]
In JavaScript, there are various ways of mapping the Object Oriented paradigm to language constructs. mxGraph uses a particular scheme throughout the project, with the following implicit rules:
- Do not change the built-in prototypes
- Do not try to limit the power of the JavaScript language.
- mapping /'maep/ 映射
- paradigm /'paerdam/ 范式
- constructs /'knstrkts/ 结构
- implicit /m'plst/ 隐式的
- prototypes /'prottaps/ 原型
[翻译]
mxGraph 中有两种类型的“类”:类和单例(只有一个类实例存在)。单例映射到全局对象,其中变量名称与类名称相同。例如,mxConstants 是一个对象,所有常量定义为对象字段。普通类映射到构造函数和定义实例字段和方法的原型。例如,mxEditor 是一个函数,mxEditor.prototype 是 mxEditor 函数创建的对象的原型。mx 前缀是 mxGraph 包中所有类的命名约定,以避免与全局命名空间中的其他对象冲突。
[原文]
There are two types of “classes” in mxGraph; classes and singletons (where only one instance of the class exists). Singletons are mapped to global objects where the variable name is the same as the class name. For example, mxConstants is an object with all the constants defined as object fields. Normal classes are mapped to a constructor function and a prototype which defines the instance fields and methods. For example, mxEditor is a function and mxEditor.prototype is the prototype for the object that the mxEditor function creates. The mx prefix is a convention that is used for all classes in the mxGraph package to avoid conflicts with other objects in the global namespace.
- singletons /'sɡltnz/ 单例
- mapped /maept/ 映射
- constants /'knstnts/ 常量
- prototype /'prottap/ 原型
- conflicts /'knflkts/ 冲突
[翻译]
对于子类化,超类必须提供一个无参数或能处理无参数调用的构造函数。此外,在扩展原型后必须重新定义特殊的构造函数字段。例如,mxEditor 的超类是 mxEventSource。这在 JavaScript 中通过首先“继承”超类的所有字段和方法来表示,例如通过将原型分配给超类的一个实例:
[原文]
For subclassing, the superclass must provide a constructor that is either parameterless or handles an invocation with no arguments. Furthermore, the special constructor field must be redefined after extending the prototype. For example, the superclass of mxEditor is mxEventSource. This is represented in JavaScript by first “inheriting” all fields and methods from the superclass by assigning the prototype to an instance of the superclass, eg.
- subclassing /'sbklaes/ 子类化
- superclass /'suprklaes/ 超类
- constructor /kn'strktr/ 构造函数
- invocation /nv'ken/ 调用
- inheriting /n'hert/ 继承
mxEditor.prototype = new mxEventSource()
[翻译]并使用以下方式重新定义构造函数字段:
[原文] and redefining the constructor field using:
mxEditor.prototype.constructor = mxEditor
[翻译]
后者规则的应用是为了通过 mxUtils.getFunctionName(obj.constructor) 获取对象的构造函数名称来检索对象的类型。
[原文]
The latter rule is applied so that the type of an object can be retrieved via the name of it’s constructor using mxUtils.getFunctionName(obj.constructor).
- retrieved /r'trivd/ 检索
- constructor /kn'strktr/ 构造函数
Constructor/构造函数
[翻译]
在 mxGraph 中进行子类化时,应应用相同的机制。例如,要对 mxGraph 类进行子类化,首先必须为新类定义一个构造函数。构造函数使用 mxGraph 函数对象上的 call 方法调用超构造函数,明确传递所有参数:
[原文]
For subclassing in mxGraph, the same mechanism should be applied. For example, for subclassing the mxGraph class, first a constructor must be defined for the new class. The constructor calls the super constructor with any arguments that it may have using the call function on the mxGraph function object, passing along explicitly each argument:
- mechanism /'meknzm/ 机制
- constructor /kn'strktr/ 构造函数
- super /'supr/ 超
- arguments /'ɑrɡjmnts/ 参数
- explicitly /k'splstli/ 明确地
function MyGraph(container)
{
mxGraph.call(this, container);
}
[翻译] MyGraph 的原型继承自 mxGraph,具体如下。通常,在扩展超类之后,会重新定义构造函数:
[原文] The prototype of MyGraph inherits from mxGraph as follows. As usual, the constructor is redefined after extending the superclass:
MyGraph.prototype = new mxGraph();
MyGraph.prototype.constructor = MyGraph;
[翻译]
您可能希望在上述代码后定义与该类关联的编解码器(参见手册的 I/O 部分)。此代码将在类加载时执行,确保使用相同的编解码器对 mxGraph 和 MyGraph 的实例进行编码。
[原文]
You may want to define the codec associated for the class after the above code (see I/O section of manual). This code will be executed at class loading time and makes sure the same codec is used to encode instances of mxGraph and MyGraph.
- codec /'kodek/ 编解码器
- associated /'sosietd/ 关联的
- executed /'ekskjutd/ 执行
- encode /n'kod/ 编码
- instances /'nstnsz/ 实例
var codec = mxCodecRegistry.getCodec(mxGraph);
codec.template = new MyGraph();
mxCodecRegistry.register(codec);
Functions/函数
[翻译]
在 MyGraph 的原型中,可以按以下方式扩展 mxGraph 的函数:
[原文]
In the prototype for MyGraph, functions of mxGraph can be extended as follows.
- prototype /'prottap/ 原型
- extended /k'stendd/ 扩展
MyGraph.prototype.isSelectable = function(cell)
{
var selectable = mxGraph.prototype.isSelectable.apply(this, arguments);
var geo = this.model.getGeometry(cell);
return selectable &&(geo == null || !geo.relative);
}
[翻译]
第一行的超类调用是可选的。它使用 mxGraph 原型的 isSelectable 函数对象上的 apply 函数完成,使用特殊的 this 和 arguments 变量作为参数。只有在超类中函数未被替换的情况下,调用超类函数才可能,如下所示,这是 JavaScript 中“子类化”的另一种方式。
[原文]
The supercall in the first line is optional. It is done using the apply function on the isSelectable function object of the mxGraph prototype, using the special this and arguments variables as parameters. Calls to the superclass function are only possible if the function is not replaced in the superclass as follows, which is another way of “subclassing” in JavaScript.
- supercall /'suprkl/ 超类调用
- optional /'ɑpnl/ 可选的
- apply /'pla/ 应用
- replaced /r'plest/ 替换
- subclassing /'sbklaes/ 子类化
mxGraph.prototype.isSelectable = function(cell)
{
var geo = this.model.getGeometry(cell);
return selectable && (geo == null || !geo.relative);
}
[翻译]
上述方案在需要完全替换函数定义时很有用。
[原文]
The above scheme is useful if a function definition needs to be replaced completely.
- scheme /skim/ 方案
- definition /def'nn/ 定义
- replaced /r'plest/ 替换
[翻译]
为了向子类添加新函数和字段,使用以下代码。下面的示例添加了一个新函数以返回图模型的 XML 表示:
[原文]
In order to add new functions and fields to the subclass, the following code is used. The example below adds a new function to return the XML representation of the graph model:
- subclass /'sbklaes/ 子类
- fields /fildz/ 字段
- representation /reprzen'ten/ 表示
MyGraph.prototype.getXml = function()
{
var enc = new mxCodec();
return enc.encode(this.getModel());
}
Fields/字段
[翻译]
同样,声明和定义新字段如下:
[原文]
Likewise, a new field is declared and defined as follows:
- declared /d'klerd/ 声明
- defined /d'fand/ 定义
MyGraph.prototype.myField = ‘Hello, World!’;
[翻译]
请注意,分配给 myField 的值只创建一次,即 MyGraph 的所有实例共享相同的值。如果需要特定于实例的值,则必须在构造函数中定义该字段。例如:
[原文]
Note that the value assigned to myField is created only once, that is, all instances of MyGraph share the same value. If you require instance-specific values, then the field must be defined in the constructor instead. For example:
- assigned /'sand/ 分配
- instances /'nstnsz/ 实例
- constructor /kn'strktr/ 构造函数
function MyGraph(container)
{
mxGraph.call(this, container);
this.myField = [];
}
[翻译]
最后,使用以下代码创建 MyGraph 的新实例,其中 container 是作为图视图容器的 DOM 节点:
[原文]
Finally, a new instance of MyGraph is created using the following code, where container is a DOM node that acts as a container for the graph view:
- instance /'nstns/ 实例
- DOM /di o 'em/ 文档对象模型
- container /kn'tenr/ 容器
var graph = new MyGraph(container);
相关推荐
- SM小分队Girls on Top,女神战队少了f(x)?
-
这次由SM娱乐公司在冬季即将开演的smtown里,将公司的所有女团成员集结成了一个小分队project。第一位这是全面ACE的大姐成员权宝儿(BoA),出道二十年,在日本单人销量过千万,韩国国内200...
- 韩国女团 aespa 首场 VR 演唱会或暗示 Quest 3 将于 10 月推出
-
AmazeVR宣布将在十月份举办一场现场VR音乐会,观众将佩戴MetaQuest3进行体验。韩国女团aespa于2020年11月出道,此后在日本推出了三张金唱片,在韩国推出了...
- 韩网热议!女团aespa成员Giselle在长腿爱豆中真的是legend
-
身高163的Giselle,长腿傲人,身材比例绝了...
- 假唱而被骂爆的女团:IVE、NewJeans、aespa上榜
-
在韩国,其实K-pop偶像并不被认为是真正的歌手,因为偶像们必须兼备舞蹈能力、也经常透过对嘴来完成舞台。由于科技的日渐发达,也有许多网友会利用消音软体来验证K-pop偶像到底有没有开麦唱歌,导致假唱这...
- 新女团Aespa登时尚大片 四个少女四种style
-
来源:环球网
- 韩国女团aespa新歌MV曝光 画面梦幻造型超美
-
12月20日,韩国女团aespa翻唱曲《DreamsComeTrue》MV公开,视频中,她们的造型超美!WINTER背后长出一双梦幻般的翅膀。柳智敏笑容甜美。宁艺卓皮肤白皙。GISELLE五官精致...
- 女网友向拳头维权,自称是萨勒芬妮的原型?某韩国女团抄袭KDA
-
女英雄萨勒芬妮(Seraphine)是拳头在2020年推出的第五位新英雄,在还没有正式上线时就备受lsp玩家的关注,因为她实在是太可爱了。和其他新英雄不同的是,萨勒芬妮在没上线时就被拳头当成虚拟偶像来...
- 人气TOP女团是?INS粉丝数见分晓;TWICE成员为何在演唱会落泪?
-
现在的人气TOP女团是?INS粉丝数见分晓!现在爱豆和粉丝之间的交流方法变得多种多样,但是Instagram依然是主要的交流手段。很多粉丝根据粉丝数评价偶像的人气,拥有数百、数千万粉丝的组合作为全球偶...
- 韩国女团MVaespa Drama MV_韩国女团穿超短裙子跳舞
-
WelcometoDrama.Pleasefollow4ruleswhilewatchingtheDrama.·1)Lookbackimmediatelywhenyoufe...
- aespa师妹团今年将出道! SM职员亲口曝「新女团风格、人数」
-
记者刘宛欣/综合报导南韩造星工厂SM娱乐曾打造出东方神起、SUPERJUNIOR、少女时代、SHINee、EXO等传奇团体,近年推出的aespa、RIIZE更是双双成为新生代一线团体,深受大众与粉丝...
- 南韩最活跃的女团aespa,新专辑《Girls》即将发布,盘点昔日经典
-
女团aespa歌曲盘点,新专辑《Girls》即将发布,期待大火。明天也就是2022年的7月8号,aespa新专辑《Girls》即将发行。这是继首张专辑《Savage》之后,时隔19个月的第二张专辑,这...
- 章泽天女团aespa出席戛纳晚宴 宋康昊携新片亮相
-
搜狐娱乐讯(山今/文玄反影/图科明/视频)法国时间5月23日晚,女团aespa、宋康昊、章泽天等明星亮相戛纳晚宴。章泽天身姿优越。章泽天肩颈线优越。章泽天双臂纤细。章泽天仪态端正。女团aespa亮...
- Aespa舞台暴露身高比例,宁艺卓脸大,柳智敏有“TOP”相
-
作为SM公司最新女团aespa,初舞台《BlackMamba》公开,在初舞台里,看得出来SM公司是下了大功夫的,虽然之前SM公司新出的女团都有很长的先导片,但是aespa显然是有“特殊待遇”。运用了...
- AESPA女团成员柳智敏karina大美女
-
真队内速度最快最火达成队内首个且唯一两百万点赞五代男女团中输断层第一(图转自微博)...
- 对来学校演出的女团成员语言性骚扰?韩国这所男高的学生恶心透了
-
哕了……本月4日,景福男子高中相关人士称已经找到了在SNS中上传对aespa成员进行性骚扰文章的学生,并开始着手调查。2日,SM娱乐创始人李秀满的母校——景福高中迎来了建校101周年庆典活动。当天,S...
- 一周热门
- 最近发表
-
- SM小分队Girls on Top,女神战队少了f(x)?
- 韩国女团 aespa 首场 VR 演唱会或暗示 Quest 3 将于 10 月推出
- 韩网热议!女团aespa成员Giselle在长腿爱豆中真的是legend
- 假唱而被骂爆的女团:IVE、NewJeans、aespa上榜
- 新女团Aespa登时尚大片 四个少女四种style
- 韩国女团aespa新歌MV曝光 画面梦幻造型超美
- 女网友向拳头维权,自称是萨勒芬妮的原型?某韩国女团抄袭KDA
- 人气TOP女团是?INS粉丝数见分晓;TWICE成员为何在演唱会落泪?
- 韩国女团MVaespa Drama MV_韩国女团穿超短裙子跳舞
- aespa师妹团今年将出道! SM职员亲口曝「新女团风格、人数」
- 标签列表
-
- filter函数js (37)
- filter函数excel用不了 (73)
- 商城开发 (40)
- 影视网站免费源码最新版 (57)
- 影视资源api接口 (46)
- 网站留言板代码大全 (56)
- java版软件下载 (52)
- java教材电子课本下载 (48)
- 0基础编程从什么开始学 (50)
- java是用来干嘛的 (51)
- it入门应该学什么 (55)
- java线上课程 (55)
- 学java的软件叫什么软件 (38)
- 程序开发软件有哪些 (53)
- 软件培训 (59)
- 机器人编程代码大全 (50)
- 少儿编程教程免费 (45)
- 新代系统编程教学 (61)
- 共创世界编程网站 (38)
- 亲测源码 (36)
- 三角函数积分公式表 (35)
- 函数的表示方法 (34)
- 表格乘法的公式怎么设置 (34)
- sumif函数的例子 (34)
- 图片素材 (36)
