Property Bag in javascript

我与影子孤独终老i 提交于 2019-12-10 10:04:32

问题


I'm investigating some code outside my scope that's written in a style I've never seen before. I'm trying to understand the inner workings of the following property bag:

Setter:

props.Property(name) = val;

Getter:

val = props.Property(name);

What would you need to instantiate for the setter to function as above?

EDIT: Less simplification, this code IS successfully running on a BrowserWindow within a frame (similar to a phone environment).

var UI =
        {
            ready: function(oProps)
            {
                try
                {
                    if (oProps)
                    {
                        window.external.Property(UI.FrameWidth) = '1000';
                        window.external.Property(UI.FrameHeight) = '900';
                    }

                    window.external.Ready();
                }
                catch (e) { }
            }
    };

Thanks in advance,


回答1:


I think it might be just some weird old JScript syntax. Steering away from the "internal function returns a reference" idea from the comments, I found a question about differences between JavaScript and JScript and the only syntax difference listed is this one:

The idiom f(x) = y, which is roughly equivalent to f[x] = y.

There isn't much too find about this idiom though. However, this book about Jscript.Net briefly mentions that

you can access any expando properties using square brackets or parenthesis.

"expando" seems to be a modifier for classes which allows you to add dynamic properties.




回答2:


The code above is wrapped in a silent try catch block, so it is possible that it seems to be running successfully but actually isn't.

Try logging the caught exception in the catch block. You should get a ReferenceError if this line:

window.external.Property(UI.FrameWidth) = '1000';

is ever hit (which of course depends on the value of oProps).




回答3:


This setter approach cannot work. No matter what the function returns, if you use a function call as sole expression left-hand side (LHS) of an assignment expression, you are getting

ReferenceError: Invalid left-hand side in assignment

and the like. As the others have said, you are not seeing the ReferenceError exception because it is caught as it is being thrown and there is no code to handle it. Functions are first-class objects, but calls to them are not valid LHS values. You will see that clearly if you insert console.log(e); in the catch block.

This getter approach can work. However, if you want getters and setters, at the latest in a conforming implementation of ECMAScript Edition 5 you can have that natively:

Object.defineProperty(props, "name", (function () {
  var _value;

  return {
    set: function (value) {
      /* setter code, simple example */
      _value = String(value);
    },

    get: function () {
      /* getter code, simple example */
      return _value;
    }
  };
}()));

/* triggers the setter */
props.name = 42;

/* triggers the getter */
var x = props.name;

/* "42" */
x

“Edit 2” –

var someObjs = [{a:"",someProp:"b"}];
(function (a) { return someObjs[a]})(0).someProp = "c";
console.log(someObjs[0].someProp);

– works because the return value of the function is used LHS, but as part of another expression that evaluates to a property access (using the dot property accessor syntax). The function returns a reference to an object (to an Object instance) whose property is then accessed. The property access being the LHS of an assignment expression, the RHS value ("c") is assigned to that property.

It is functionally equivalent to

var someObjs = [
  {a: "", someProp: "b"}
];

(function () {
  return someObjs[0];
}()).someProp = "c";

console.log(someObjs[0].someProp);

and therefore to

someObjs[0].someProp = "c";
console.log(someObjs[0].someProp);

(as you can see, good code style helps a lot with understanding code)



来源:https://stackoverflow.com/questions/18838213/property-bag-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!