简体   繁体   English

将原型代码放入Flex项目的最佳位置

[英]Best place to put prototype code in Flex project

I'm making a Flex project, using the Cairngorm library, and trying to keep the code in a good MVC structure. 我正在使用Cairngorm库制作一个Flex项目,并试图将代码保持在良好的MVC结构中。 I just added some code to add a prototype function to a built-in class (I added the method "contains" to Array), and I'm wondering what you would consider the best-practice for where to put this code in my project structure? 我只是添加了一些代码以将原型函数添加到内置类中(我将“包含”方法添加到Array中),并且我想知道您将如何将此代码放在项目中的最佳实践?结构体?

In my honest opinion, it would be best to subclass or compose on Array, instead of modifying its prototype. 坦率地说,最好是在Array上进行子类化或组成,而不是修改其原型。 Modifying the prototype can lead to confusion in the maintenance phase, which is one of the main reasons for using a framework like Cairngorm. 修改原型可能导致维护阶段的混乱,这是使用Cairngorm之类的框架的主要原因之一。

If creating a new class doesn't suit you, then also consider creating a utility class. 如果创建新类不适合您,则还可以考虑创建实用程序类。

Flex actually already has a utility class called ArrayUtil and a function which does what you want: ArrayUtil.getItemIndex . Flex实际上已经有了一个名为ArrayUtil的实用程序类和一个可以满足您需要的函数: ArrayUtil.getItemIndex

var obj1:Object = new Object();
var obj2:Object = new Object();
var myArray : Array = [obj1, obj2];
ArrayUtil.getItemIndex(obj1, myArray);    // returns 0
ArrayUtil.getItemIndex(obj2, myArray);    // returns 1
ArrayUtil.getItemIndex(obj3, myArray);    // returns -1

Tutorial 讲解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM