这段代码有什么作用?我的意思是特别是原型部分。传入的chain对象是CSS名/值对的对象。我认为 defaultFactory 返回一个具有名称值对的对象。我不知道设置原型的作用。
function addDefaultStyleToProtoChain(chain):Object {
// If there's a defaultFactory for this style sheet,
// then add the object it produces to the chain.
if (defaultFactory != null)
{
var originalChain:Object = chain;
// from here on...
defaultFactory.prototype = chain;
chain = new defaultFactory();
defaultFactory.prototype = null;
}
return chain;
}
这是在 Flex 4.6 SDK 的 CSSStyleDeclaration 类的 addDefaultStyleToProtoChain 方法中
对于上下文,如果:
var o:Object = new defaultFactory();
trace(o) // {fontFamily:Arial, color:blue};
trace(chain) // {color:red, fontWeight:bold}
defaultFactory.prototype = chain;
chain = new defaultFactory();
trace(chain) // ???
链条等于什么?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~
根据下面的答案,这是怎么回事:
// create a function that returns an object
var myDynamicDefinition:Function = function() {
this.name = "initialvalue";
trace('this.name='+this.name); // returns this.name=initialvalue
}
var dynamicDefinitionInstance = new myDynamicDefinition(); // create an instance of myDynamicDefinition
trace(ObjectUtil.toString(dynamicDefinitionInstance));// returns name = "initialvalue"
// create a new definition and assign it to our dynamic definitions
var newDefinition = {color:"red", name:"newObjectValue"};
myDynamicDefinition.prototype = newDefinition;
// create a new instance with new definition
var dynamicDefinitionInstance2 = new myDynamicDefinition(); // create an instance of myDynamicDefinition
trace(ObjectUtil.toString(dynamicDefinitionInstance2)); // returns name = "initialvalue" color = "red"
// delete name property instance
delete dynamicDefinitionInstance2.name;
trace(ObjectUtil.toString(dynamicDefinitionInstance2)); // returns name = "newObjectValue" color = "red"
// set prototype to null
myDynamicDefinition.prototype = null;
// create a new instance
var dynamicDefinitionInstance3 = new myDynamicDefinition();
trace(ObjectUtil.toString(dynamicDefinitionInstance2)); // returns name = "newObjectValue" color = "red"
看起来当原型对象设置为包含同名属性的对象时,新实例上的属性值在创建新实例后不会改变。所以一开始没什么区别。如果该属性被删除,则原型对象上的值将用作 FALLBACK。请参阅上面的示例代码和返回值。