TypeScript - 对象不支持属性或方法“defineProperty”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19658392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript - Object doesn't support property or method 'defineProperty'
提问by 7dino7
I recently created a TypeScript project for a relatively complex simulation engine using Visual Studio which, by default, targeted ECMAScript 3 (ES3). I wanted to start using real properties in my TypeScript classes, so I updated my project file to target ES5, like so:
我最近使用 Visual Studio 为相对复杂的模拟引擎创建了一个 TypeScript 项目,默认情况下,该项目针对 ECMAScript 3 (ES3)。我想开始在我的 TypeScript 类中使用真实的属性,所以我更新了我的项目文件以针对 ES5,如下所示:
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<TypeScriptTarget>ES5</TypeScriptTarget>
<TypeScriptRemoveComments>false</TypeScriptRemoveComments>
<TypeScriptSourceMap>true</TypeScriptSourceMap>
<TypeScriptModuleKind>AMD</TypeScriptModuleKind>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<TypeScriptTarget>ES5</TypeScriptTarget>
<TypeScriptRemoveComments>true</TypeScriptRemoveComments>
<TypeScriptSourceMap>false</TypeScriptSourceMap>
<TypeScriptModuleKind>AMD</TypeScriptModuleKind>
</PropertyGroup>
Now when I run my application in IE (v10), I get a run-time exception: "0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'defineProperty'". If I switch to launching my application with any other browser (e.g., Firefox, Chrome), it works as expected - no errors. I can't seem to find any reason why IE isn't working as expected. I found a website that confirmed that, in general, my IE browser supports 'defineProperty', so now I'm really baffled as to why it won't work during development. This is getting particularly critical since I can't debug my TypeScript code in VS. Any thoughts?
现在,当我在 IE (v10) 中运行我的应用程序时,出现运行时异常:“0x800a01b6 - JavaScript 运行时错误:对象不支持属性或方法‘defineProperty’”。如果我切换到使用任何其他浏览器(例如,Firefox、Chrome)启动我的应用程序,它会按预期工作 - 没有错误。我似乎找不到 IE 没有按预期工作的任何原因。我找到了一个网站,确认我的 IE 浏览器通常支持“defineProperty”,所以现在我真的很困惑为什么它在开发过程中不起作用。这变得特别重要,因为我无法在 VS 中调试我的 TypeScript 代码。有什么想法吗?
回答by Fenton
According to the ES5 Compatibility table Object.defineProperty is supported in IE9 and above.
根据 ES5 Compatibility table Object.defineProperty is supported in IE9 and above。
Support in IE8 is limited.
IE8 中的支持是有限的。
However, IE10 has a habit of running local and intranet pages in compatibility mode, even though the same pages run in normal mode over the Internet.
但是,IE10 有在兼容模式下运行本地和内网页面的习惯,即使相同的页面在 Internet 上以正常模式运行。
You can change this in the dialog in Tools > Compatibility View Settings (remove the "Display intranet sites in compatibility mode" option. You can also prevent this using a combination of the correct doctype:
您可以在“工具”>“兼容性视图设置”的对话框中更改此设置(删除“以兼容模式显示 Intranet 站点”选项。您也可以使用正确的文档类型的组合来防止这种情况:
<!DOCTYPE html>
Or a user-agent compatibility tag, which will no longer be required (or supported) in IE11 and above.
或者用户代理兼容性标签,在 IE11 及更高版本中将不再需要(或支持)。
<meta http-equiv="x-ua-compatible" content="IE=edge">
This must be the first tag within the <head>
element.
这必须是<head>
元素中的第一个标签。
回答by basarat
Make sure that the first line of your HTML file is
确保 HTML 文件的第一行是
<!doctype html>
This will prevent IE from going in compatibility mode and disable ES5 features.
这将阻止 IE 进入兼容模式并禁用 ES5 功能。