简体   繁体   English

什么是变量“外部”意味着在javascript中

[英]what is variable “external” meant for in javascript

I just found out that there is a variable called external exists in most browsers except IE. 我发现除了IE之外,大多数浏览器中都存在一个名为external的变量。 I have 2 question wrt to this 我有2个问题

  1. what is external - http://jsfiddle.net/EVBjU/ 什么是外部的 - http://jsfiddle.net/EVBjU/
  2. IE gives object doesn't support this property or method when i do console.log(external). 当我执行console.log(外部)时,IE给object doesn't support this property or method how to fix this, considering it's just a variable 如何解决这个问题,考虑到它只是一个变量

Thanks 谢谢

"but how do i fix a "object doesn't support this property or method" in general" “但我如何修复”对象不支持此属性或方法“一般”

Given an object obj , you can test whether property/method prop exists with: 给定一个对象obj ,您可以测试属性/方法prop是否存在:

if ("prop" in obj) {
    // do something with obj.prop
}

...noting that the in operator will check inherited properties too. ...注意到in运算符也会检查继承的属性。 To check only for direct properties use: 要仅检查直接属性,请使用:

if (obj.hasOwnProperty("prop")) {
    // do something with obj.prop
}

"is there a way to check if the variable external exists" “有没有办法检查变量external存在”

In the case of the external property you mentioned, it will be a property of window if it exists, so: 对于您提到的external属性,如果存在,它将是window的属性,因此:

if ("external" in window) {
   // do something
}

This x in window technique works for global variables including ones provided by the browser and user-defined ones. 这种x in window技术适用于全局变量,包括浏览器和用户定义的变量。 It doesn't work on local variables. 它不适用于局部变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM