Skip to content

Instantly share code, notes, and snippets.

@lynxerzhang
Created October 31, 2012 16:03
Show Gist options
  • Save lynxerzhang/3987902 to your computer and use it in GitHub Desktop.
Save lynxerzhang/3987902 to your computer and use it in GitHub Desktop.
关于as3中in操作符的使用问题
in 用于在诸如Dictionary Object 对象中尝试返回指定键是否存
var d = new Dictionary();
var s = new Sprite();
d[s] = true;
trace(s in d); // true
但目前假设d对象是自定义类, 并且覆盖了Object的toString()方法,
同时在toString()方法中, 正巧有访问为空的自定义属性,而s又正好不是d
对象的键, 那么此时居然会触发TypeError空对象错误, 解决方法是使用[]
操作符, 或者使用try...catch...预先处理下try {s in d}。
in 用于在诸如Dictionary Object 对象中尝试返回指定键是否存
var d = new Dictionary();
var s = new Sprite();
d[s] = true;
trace(s in d); // true
但目前假设d对象是自定义类, 并且覆盖了Object的toString()方法,
同时在toString()方法中, 正巧有访问为空的自定义属性,而s又正好不是d
对象的键, 那么此时居然会触发TypeError空对象错误, 也许是内部调用了toString()
方法, 解决方法是使用[]操作符, 或者使用try...catch...预先处理下try {s in d}。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment