Git Product home page Git Product logo

Comments (23)

dmikis avatar dmikis commented on May 27, 2024

Agreed. Actually, I was a little surprised that the code style forbids checking for undefined via typeof.

from mapsapi-codestyle.

ikokostya avatar ikokostya commented on May 27, 2024

Global undefined is immutable:

> undefined = 1;
1
> undefined
undefined

from mapsapi-codestyle.

twirl avatar twirl commented on May 27, 2024

Which browser did you use?

from mapsapi-codestyle.

ikokostya avatar ikokostya commented on May 27, 2024

Which browser did you use?

chrome, ff, opera 12.16

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined

In modern browsers (JavaScript 1.8.5 / Firefox 4+), undefined is a non-configurable, non-writable property per the ECMAScript 5 specification.

It should work in IE9+.

from mapsapi-codestyle.

twirl avatar twirl commented on May 27, 2024

Aaaaaand what to do if you need to support IE8?

from mapsapi-codestyle.

ikokostya avatar ikokostya commented on May 27, 2024

If you write some js library and you don't known target enviroment (es5 or es3), you can

  1. use typeof everywhere
  2. wrap all your code in
(function (undefined) {

// code here

}());
  1. use es5 to es3 transformer like es3fy

from mapsapi-codestyle.

twirl avatar twirl commented on May 27, 2024
  1. is obviously better, isn't it?

from mapsapi-codestyle.

ikokostya avatar ikokostya commented on May 27, 2024

I think 3) is better. IE8 has very small percent usage. It shouldn't change style guide.
BTW In next year es6 standard should be released.

from mapsapi-codestyle.

 avatar commented on May 27, 2024

I think 1) is better. This is more safely and readable.

from mapsapi-codestyle.

alt-j avatar alt-j commented on May 27, 2024

We can make clause about IE8 and typeof.

from mapsapi-codestyle.

tarmolov avatar tarmolov commented on May 27, 2024

I think it's not bad to add exception for projects which support IE8.

from mapsapi-codestyle.

alt-j avatar alt-j commented on May 27, 2024

@twirl, can you add this exception?

from mapsapi-codestyle.

twirl avatar twirl commented on May 27, 2024

I'd prefer to have this a common rule, not exception. It's just semantically more correct (you're checking type of variable) and safer.

from mapsapi-codestyle.

dfilatov avatar dfilatov commented on May 27, 2024

What should I do with this absolutely strange rule if I wanna do something like this:

switch(typeof myVar) {
    case 'undefined':
        doSomething1();
        break;

    case 'string':
        doSomething2();
        break;

    case 'number':
        doSomething3();
        break;
}

?

from mapsapi-codestyle.

tarmolov avatar tarmolov commented on May 27, 2024

Hm. Let's keep it at developer's discretion. IMHO this rule doesn't make a big sense.
Vote for removing the rule from the codestyle.

from mapsapi-codestyle.

vtambourine avatar vtambourine commented on May 27, 2024

@tarmolov 👍

from mapsapi-codestyle.

dodev avatar dodev commented on May 27, 2024

+1 for removing it.

from mapsapi-codestyle.

twirl avatar twirl commented on May 27, 2024

This rule does exactly the same sense as explicit type conversions via Boolean, Number, String, etc.
All types shall be cast uniformly via constructor functions and checked uniformly via typeof operator.

from mapsapi-codestyle.

ikokostya avatar ikokostya commented on May 27, 2024

This rule does exactly the same sense as explicit type conversions via Boolean, Number, String, etc.

This is not true for null. typeof null === 'object'. I don't see difference between null and undefined in this case.

from mapsapi-codestyle.

twirl avatar twirl commented on May 27, 2024

typeof null is the most famous example of JavaScript design flaw. In ES6 Type(null) == 'null'
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-terms-and-definitions-null-type

from mapsapi-codestyle.

ikokostya avatar ikokostya commented on May 27, 2024

In es6 typeof null returns 'object' http://people.mozilla.org/~jorendorff/es6-draft.html#sec-typeof-operator-runtime-semantics-evaluation

from mapsapi-codestyle.

twirl avatar twirl commented on May 27, 2024

Of course, because of backwards compatibility.

from mapsapi-codestyle.

ikokostya avatar ikokostya commented on May 27, 2024

http://es-discourse.com/t/why-typeof-is-no-longer-safe/15

from mapsapi-codestyle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.