W3C home > Mailing lists > Public > public-device-apis-log@w3.org > December 2016

Re: [sensors] Don't allocate response objects to avoid triggering GC

From: moka via GitHub <sysbot+gh@w3.org>
Date: Wed, 07 Dec 2016 12:37:30 +0000
To: public-device-apis-log@w3.org
Message-ID: <issue_comment.created-265436714-1481114249-sysbot+gh@w3.org>
If sensor has `reading` property, which is an object, with properties,
 then developer in event might assume it is immutable and/or 
instanced. This could lead to confusion like:

```javascript
var lastReading = null;
accelerometer.onchange = function(reading) {
    if (lastReading) {
        var deltaX = reading.x - lastReading.x;
    }
    lastReading = reading;
}
```
Someone would be confused why this wouldn't work on assumption that 
reading was immutable objects.

But if there is no object involved, such as `sensor.x`, then:
```javascript
var lastX = null;
accelerometer.onchange = function() {
    if (lastX !== null) {
        var deltaX = this.x - lastX;
    }
    lastX = this.x;
}
```

There is much less chance to get confused in this case.

-- 
GitHub Notification of comment by Maksims
Please view or discuss this issue at 
https://github.com/w3c/sensors/issues/153#issuecomment-265436714 using
 your GitHub account
Received on Wednesday, 7 December 2016 12:37:37 UTC

This archive was generated by hypermail 2.3.1 : Monday, 23 October 2017 12:18:52 UTC