简体   繁体   English

检查JavaScript中是否存在属性

[英]Checking existence of properties in JavaScript

I'm new to JavaScript and a little bit confused with the duck typing concept. 我是JavaScript新手,对鸭子打字概念有点困惑。 As far as I can tell, I understood the concept. 据我所知,我理解这个概念。 But that leads to a strange consequence in my thoughts. 但这导致了我思想中的奇怪后果。 I will explain with the following example: 我将用以下示例解释:

I'm currently working on a mobile web app with jQuery Mobile. 我目前正在使用jQuery Mobile开发移动网络应用程序。 At one point I capture the vmousedown event for a canvas. 有一次我捕获了画布的vmousedown事件。 I'm interested in the pressure of the touch. 我对触摸的压力很感兴趣。 I found the Touch.webkitForce property. 我找到了Touch.webkitForce属性。

$('#canvas').live('vmousedown', function(e){
    console.log(e.originalEvent.originalEvent.touches[0].webkitForce);
}

This works fine when using the Remote Debugging for Chrome. 使用远程调试 Chrome时,此工作正常。 But throws an exception when testing in Opera Firefly, because the originalEvent property is no touch event, but a click event. 但是在Opera Firefly中测试时抛出异常,因为originalEvent属性不是触摸事件,而是click事件。

So every time I access a property of an object which is not under my authority, do I have to check existence and type? 所以每当我访问不属于我权限的对象的属性时,我是否必须检查存在并键入?

if( e.originalEvent &&
    e.originalEvent.originalEvent &&
    e.originalEvent.originalEvent.touches && 
    e.originalEvent.originalEvent.touches[0] && 
    e.originalEvent.originalEvent.touches[0].webkitForce) {

    console.log(e.originalEvent.originalEvent.touches[0].webkitForce);
}

Can please someone clarify that for me? 可以请有人为我澄清一下吗?

So every time I access a property of an object which is not under my authority, do I have to check existence and type? 所以每当我访问不属于我权限的对象的属性时,我是否必须检查存在并键入?

Yes you will have to check the whole path, once at a time, or you can automate it: 是的,你必须一次检查整个路径,或者你可以自动化它:

function deepObject(o, s) {
    var ss = s.split(".");

    while( o && ss.length ) {
        o = o[ss.shift()];
    }

    return o;
}

var isOk = deepObject(e, "originalEvent.originalEvent.touches.0.webkitForce");

if ( isOk ) {
    // isOk is e.originalEvent.originalEvent.touches.0.webkitForce;
}

Test case: 测试用例:

var o = {
  a: {
    b: {
      c: {
        d: {
          e: {
          }
        }
      }
    }
  }
}

var a = deepObject(o, "a.b.c");
var b = deepObject(a, "d");

console.log(a); // {"d": {"e": {}}}
console.log(b); // {"e": {}}
console.log(deepObject(o, "1.2.3.3")); // undefined

Use try catch 使用try catch

$('#canvas').live('vmousedown', function(e) {
   try {
       console.log(e.originalEvent.originalEvent.touches[0].webkitForce);
   } catch(e) {
       console.error('error ...');
   }
}

As you are using a specific framework to capture your events, i think that you should assume that the originalEvent is always defined. 当您使用特定框架捕获事件时,我认为您应该假设始终定义originalEvent。 If it isn't, then it is probably a good thing to throw an error as something clearly went wrong somewhere in the capture of the event. 如果不是,那么抛出错误可能是一件好事,因为事件捕获中的某些地方显然出现了问题。

However, the event could be a MouseEvent or a TouchEvent , also, the webkitForce property may not be supported. 但是,事件可能是MouseEventTouchEvent ,也可能不支持webkitForce属性。 These are the kind of cases that you might want to detect : 这些是您可能想要检测的案例:

// assume that originalEvent is always be defined by jQuery
var originalEvent = e.originalEvent.originalEvent;
if (originalEvent instanceof TouchEvent) {  // if touch events are supported
  // the 'touches' property should always be present in a TouchEvent
  var touch = originalEvent.touches[0];
  if (touch) {
      if (touch.webkitForce) {
        // ...
      } else {
        // webkitForce not supported
      }
  }  // else no finger touching the screen for this event
} else {
   // probably a MouseEvent
}

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

相关问题 在JavaScript中检查没有hasOwnProperty的哈希键的存在 - Checking existence of hash key without hasOwnProperty in JavaScript 通过javascript检查表中给定类的输入是否存在 - checking the existence of input with a given class in the table by javascript 检查对象javascript中是否存在嵌套属性 - Checking existence of nested property in an object javascript JavaScript中某个方法的存在:检查真实性是否足够? - Existence of a method in JavaScript: Checking for truthy sufficient? 使用javascript测试JSON中是否存在属性 - Testing for existence of a properties in JSON using javascript 在JavaScript中检查对象是否存在后如何跳过循环 - How to skip a loop after checking existence in an object in JavaScript Javascript-在检查对象属性存在时避免异步竞争条件 - Javascript - avoiding asynchronous race condition when checking object property existence 使用客户端javascript检查注册表项的存在 - checking existence of a registry key using client side javascript 根据特定值检查 Javascript 中的数组中是否存在 object - Checking existence of object in Array in Javascript based on a particular value 在 javascript 中,是否存在嵌入式 object 每一层的语法快捷方式检查? - in javascript, is there syntatic shortcut checking existence of each layer of embedded object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM