Are there legitimate uses for JavaScript's “with” statement?

后端 未结 30 2121
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 04:22

Alan Storm\'s comments in response to my answer regarding the with statement got me thinking. I\'ve seldom found a reason to use this particular language feature, and had ne

30条回答
  •  没有蜡笔的小新
    2020-11-22 05:18

    You can define a small helper function to provide the benefits of with without the ambiguity:

    var with_ = function (obj, func) { func (obj); };
    
    with_ (object_name_here, function (_)
    {
        _.a = "foo";
        _.b = "bar";
    });
    

提交回复
热议问题