如何将Object.defineProperty在AS3? [英] How would Object.defineProperty be in AS3?

查看:162
本文介绍了如何将Object.defineProperty在AS3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自一个强大的JavaScript背景的建筑师,但我做了一些.NET和Java的过去。

I'm an architect from a strong JavaScript background, but I did some .NET and Java in the past.

不过,我希望把一只手搭在ActionScript3的,这我当时答应了,这是非常相关的JavaScript。

However, I wanted to put a hand on ActionScript3, which I was promised that is very related to JavaScript.

作为启动项目,我把我自己尝试端口的ActionScript3我最喜欢的断言utils的一 - 应该。 JS - 这使得您的测试codeS真正愉快的阅读

As a startup project I took on myself to try port to ActionScript3 one of my favorite assertion utils - should.js - that makes your test codes really pleasant to read.

更新时间:2013年2月19日

我看到我混淆我的抽象的讲,所以我更换了一些职位在考虑具体的问题。 下面是完整的画面:

I saw I confuse with my abstract speaking, so I replaced some of the post with the concrete question in mind. Here's the full picture:

考虑下面的JavaScript code:

Consider the following JavaScript code:

Object.defineProperty(Object.prototype, 'should'
, { set: function(){}
  , get: 
    function(){
       return new Assertion(Object(this).valueOf());
    }
  , configurable: true
  , enumerable  : false
  }
);

这是JavaScript模块应该的实施的一部分。另一部分是与价值构成的类断言的定义,并实现了宽而漂亮的一套断言方法,针对该值。像类似方法

That is part of the implementation of the JavaScript module Should. The other part is a definition of a the class Assertion, that is constructed with a value, and implements a wide and nice set of assertion methods, against that value. Methods like like

var o = Assertion(actualValue)
o.equals(expectedValue1)
o.moreThan(expectedValue2)
o.contains(expectedValue3)

和别名,以保持英语语法

and aliases to keep english grammer

var o = Assertion(actualValue)
o.equal(expectedValue1)
o.contain(expectedValue3)

和别名懒神枪手,像

o.eql(expectedValue)
o.gt(expectedValue) //greater then
o.gte(...) //greater then or equal
//and so on... 

和一些连接器,只返回,(这是断言的实例与测试值构造)像

and some connectors that just return this, (which is the instance of Assertion constructed with the test value) like

o.be
o.and

这是什么给你?

What does it give you?

var person = getPerson();
Should.exist(person); //that's a static call, and that's easy

//but these are a member calls:
person.should.have("name","age","address","friends");  
person.name.should.equal("John");
person.age
  .should
      .be.number()
  .and.be.between(20,30);

person.address
  .should
    .be.string().and
    .startWith("\d").and
    .endWith(" st.")
  //or even
    .and.match(/^[0-9]{1,9}\s+[A-Z][a-z0-9 ]* st\.$/);

person.friends
  .should
    .be.array().and
    .be.between(3,5).and
    .containOnlyType(String);

这不是太神奇了吗?这是简单的英语!

Isn't that wonderful? it's plain English!

您可以争论的压痕,哪里放的美学,如果他们是在所有必要的,但除此之外 - 任何人都可以读或写: 一旦你采取了应当属性,它存在于的每次的对象,但不会破坏地图迭代 - 你可以去链接不管你有权要求对于你从开始值

You could argue about aesthetics of indentation, where to put the and, and if they are at all necessary, but besides that - anybody can read or write it: Once you took the 'should' attribute that exists on every object but does not spoil map iterations - you can go on chaining whatever you have to claim regarding the value you started from.

这可能有更多漂亮的迭代工具,反映公共事业,与测试功能相关的对象模型,等等等等增强,但让刚刚拿到了第一步:)

It could have more nifty iteration tools, reflection utilities, be augmented with test functions relevant for your object model, and so on and so forth, but lets just get over the first step :)

但是,对于这一点,你需要每一个对象在系统中配备了不可枚举智能叫物业,在它的 的getter 函数返回构造一个断言对象作为测试值。

But for that, you need every object in the system to feature a non-enumerable smart property called should that in it's getter function returns an Assertion object constructed with the this as the tested value.

(你是不是看到什么,但 - 等待,看看美丽的拒绝的消息它给Yummie!! 所以啊 - 我将愉快地牺牲选项调用属性应该......并愉快地放弃智能感知,以及 - 至少,只要它是纯英文)

(you ain't seen nothing yet - wait to see the beautiful rejection messages it gives! Yummie!! So yea - I would happily sacrifice the option to call an attribute "should"... and will happily give up intelisense as well - at least as long as it's plain English)

所以,在注释中, bfavaretto 给我们的第一步 - 我们知道如何prevent属性的枚举 - 大和放大器;谢谢!

So, in comments, bfavaretto gave us the first step - we know how to prevent enumeration of an attribute - great & thanks!!

现在,我们可以把它一个getter属性谁的功能,可以访问

Now, can we make it a getter-attribute who's function can access the this?

当我做完了,我打算把它在一些公共回购MIT许可下,对我们所有人来说有乐趣:)

When I'm done I'm going to put it in some public repo licensed under MIT, for all of us to have fun with :)

帮助别人?

推荐答案

好了,伙计们,感谢所有帮助,22+ 我给为那些有兴趣在原来问题的人的总结,之后 - 我会告诉你我的努力成果

Ok, guys, thanks for all the help, 22+ I'll give a summary for the people that are interested in the original question, and after that - I'll show you the outcome of my efforts.

的challange制成两部分组成:

The challange was made of two parts:

1 - prevent扩充(=加在运行时)财产被枚举

要第一部分 - 感谢@bfavaretto,谈到这个问题的水平 - Object.setPropertyIsEnumerable - 奏效了伟大

To the first part - thanks to @bfavaretto, commented on the question level - Object.setPropertyIsEnumerable - did the trick great.

2 - 使增强性操作getter函数能够访问,以便它可以在返回值的构造函数使用它

2 - make the augmented property operate a getter function with access to the this so it can use it on the constructor of the returned value.

关于本第二部分 - 基本上 - 我无法找到一个方法来增加(=添加)属性获取一个原型,并使其在该享受它是通过继承树API实例操作

About this second part - Basically - I could not find a way to augment (=add) a property getter to a prototype, and have it operate on instances that enjoy it's API through the inheritance tree.

总之,在这些限制 - 这里的结果:

Anyway, within these limits - here's the outcome:

不准确的移植,因为平台的差异, 我仍然有一些方法,以赶上原should.js(如HTTP测试方法) 但足够接近。 的主要区别在于,代替

Not exact porting because of the platform differences, and I still have some methods to catch up with the original should.js (like the HTTP testing methods) but close enough. The main difference is that instead

var o:Object = 
    { name : "Radagast"
    , color: "Brown"
    }
o.should.have.properties("name","color")
    .and.have.property("name","Radagast");
o.name.should.not.equal("Palandoo");
o.color.should.equal("Brown");

您必须去

o.should().have.properties("name","color")
       and.have.property("name","Radagast");
o.name.should().not.equal("Palandoo");
o.color.should().equal("Brown");

(括号 - 没有消气可能的 - 这样的属性应该是一个方法,你必须自己调用它)

(the brackets - no getter possible - so the should attribute is a method, and you have to invoke it yourself)

现在,如果你碰到困难和需要帮助的智能感知,你必须要做到这一点:

Now if you get stuck and need help from the intellisense, you have to do this:

var should:tdd.Should = o.color.should();
should. <ctrl+space>

这kind'a取刺出,但在智能感知偷看 - 它帮助

which kind'a takes the sting out, but for a peek in the intelisense - it helps

还有一件事 - 你必须强迫应该在静态构造函数,一旦在执行中,你可以, 例如,我在这里:

One more thing - you have to force the static constructor of Should as soon in execution as you can, for example, I do it here:

[Suite]
[RunWith("org.flexunit.runners.Suite")]
public class my_awsome_test_suite
{
            //forces the static constructor of tdd.Should
    import tdd.Should;
    private static var s:Should = new Should(); 

    public var c1:testCase1;
    public var c2:testCase2;
    public var c3:testCase3;
    public var c4:testCase4;
    }

我可能会添加一些propper后README.md,更要命的成员函数 tdd.Should

玩得开心

这篇关于如何将Object.defineProperty在AS3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆