microsoft / Chakra-Samples

Repository for Chakra JavaScript engine related samples.
MIT License
216 stars 84 forks source link

Strict rules #15

Closed SrinivasGourarum closed 8 years ago

SrinivasGourarum commented 8 years ago

Hi Limin,

When trying to set a property to a non-writable property and specifying "use strict" in the js code, we get an exception as expected. Is it possible to achieve this natively without writing "use strict" in the js code?

In JsSetProperty and JsDeleteProperty, there is an argument "bool useStrictRules". Is this related to "use strict"?

Regards, Srinivas.

liminzhu commented 8 years ago

Right. If you have useStrictRules=true, you will be setting or deleting properties in strict mode.

SrinivasGourarum commented 8 years ago

Please check the application uploaded @ https://onedrive.live.com/redir?resid=9C4512A9CBBBEB98!110&authkey=!AKoUt3w5HAkamno&ithint=file%2czip

I have used "useStrictRules = true" while defining the property. There is no exception thrown when the below code is executed.

var btn = new Button(); btn.myProperty = "new Value";

I am missing something here?

liminzhu commented 8 years ago

I see. Think of useStrictRules=true as a "use strict" declaration in the scope of the function that has useStrictRules as an argument, and strict mode only applies to that function. For example, when you do,

propertyDesc.SetProperty(enumerableProp, enumerableVal, true);//used strict rules

you are setting enumerableProp under strict mode; however, strict mode won't persist after the SetProperty call. Thus, when btn.myProperty = "new Value"; is executed, it's not in strict mode and no exception is thrown.

liminzhu commented 8 years ago

If you want to enforce strict mode, the recommended way is still adding "use script" at the top of your script. One more thing to notice is that if you have useStrictRules=false, that means you that don't additionally add the "use script" declaration in your setting/deletion operation, not necessarily that the operation is not in strict mode. If the script declares strict mode, the property setting/deletion is still operated under strict mode even if useStrictRules=false. Hope the explanation is clear.

SrinivasGourarum commented 8 years ago

Thanks for the elaborate explanation. For us, the exception needs to be thrown even when "use strict" is not added in the script. As a workaround, I could define the setter callback for the read-only properties as well and then throw an exception from there.