Comments (23)
Agreed. Actually, I was a little surprised that the code style forbids checking for undefined
via typeof
.
from mapsapi-codestyle.
Global undefined is immutable:
> undefined = 1;
1
> undefined
undefined
from mapsapi-codestyle.
Which browser did you use?
from mapsapi-codestyle.
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.
Aaaaaand what to do if you need to support IE8?
from mapsapi-codestyle.
If you write some js library and you don't known target enviroment (es5
or es3
), you can
- use
typeof
everywhere - wrap all your code in
(function (undefined) {
// code here
}());
- use es5 to es3 transformer like es3fy
from mapsapi-codestyle.
- is obviously better, isn't it?
from mapsapi-codestyle.
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.
I think 1) is better. This is more safely and readable.
from mapsapi-codestyle.
We can make clause about IE8 and typeof.
from mapsapi-codestyle.
I think it's not bad to add exception for projects which support IE8.
from mapsapi-codestyle.
@twirl, can you add this exception?
from mapsapi-codestyle.
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.
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.
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.
from mapsapi-codestyle.
+1 for removing it.
from mapsapi-codestyle.
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.
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.
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.
In es6
typeof null
returns 'object'
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-typeof-operator-runtime-semantics-evaluation
from mapsapi-codestyle.
Of course, because of backwards compatibility.
from mapsapi-codestyle.
http://es-discourse.com/t/why-typeof-is-no-longer-safe/15
from mapsapi-codestyle.
Related Issues (20)
- Обязательный вызов конструктора родителя в конструкторе дочернего класса HOT 1
- Codestyle for ES6 HOT 3
- Enums code style HOT 12
- Spaces around comma HOT 3
- Required space in class methods names HOT 6
- JSCS 3 support for loris preset HOT 2
- Update eslint rules HOT 3
- Clarifying object notation HOT 2
- Class properties order HOT 5
- Type assertions style
- Move react preset to separate package
- Add tslint config
- Add rule for beginning underscore for private fields HOT 1
- Mandatory return type for functions and methods HOT 3
- Identifier references in object literals HOT 7
- Template parameter naming HOT 9
- Add rule for spaces in imports HOT 2
- Ternary operator HOT 4
- async without await HOT 1
- Use tslint "max-line-length" rule HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mapsapi-codestyle.