20. Mai 2010

Zeilenumbrüche im VS2008 für Javascript einstellen

Ich arbeite mit dem Visual Studio 2008 liebe es, wenn der Code automatisch eingerückt wird, wenn man beispielsweise eine geschweifte Klammer schließt.

Allerdings ist für standardmäßig so eingestellt, das für Javascript der Indent style "K&R style" verwendet wird (http://en.wikipedia.org/wiki/Indent_style). Ich würde aber gerne den "Allman style" verwenden.

Lange Rede kurzer Sinn, man gehe im VS2008 auf "Extras" --> "Optionen" und wählt dann die Option "Text-Editor" --> "Jscript" --> "Formatierung" aus. Dort werden 2 Haken gesetzt für:

- Öffnende geschweifte Klammer für Funktionen in neuer Zeile platzieren
- Öffnende geschweifte Klammer für Kontrollblöcke in neuer Zeile platzieren


Tada - schon wird der Code im für mich perfekten Style eingerückt :-)

5. Mai 2010

Javascript - parseInt - Problem

Durch Zufall habe ich festgestellt, das die Javascript-Methode parsteInt(string, radix) eine Zahl als Octal ansieht, wenn der String z.B: "012" lautet, liefert ein parseInt("012") eine 10.

Wenn man ein decimal parsen möchte, sollte man also parseInt("012", 10) aufrufen.


Siehe dazu auch hier http://www.w3schools.com/jsref/jsref_parseInt.asp


JavaScript parseInt() Function

--------------------------------------------------------------------------------
JavaScript Global Functions
--------------------------------------------------------------------------------

Definition and Usage
The parseInt() function parses a string and returns an integer.

The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.

If the radix parameter is omitted, JavaScript assumes the following:

•If the string begins with "0x", the radix is 16 (hexadecimal)
•If the string begins with "0", the radix is 8 (octal). This feature is deprecated
•If the string begins with any other value, the radix is 10 (decimal)

Syntax
parseInt(string, radix)










ParameterDescription
stringRequired. The string to be parsed
radixOptional. A number (from 2 to 36) that represents the numeral system to be used