Archive for February, 2008

JavaScript Programmer’s Reference Where this causes particular problems

Friday, February 29th, 2008

JavaScript Programmer’s Reference Where this causes particular problems is in the maintenance phase where you might perhaps be adding another line of code. In cutting and pasting an existing line, it can be easy to overlook an operator and accidentally increment something twice or assign a value inadvertently. Special care is necessary with iterators and conditional execution blocks. A particularly nasty habit is to have a condition that when true, executes one statement. This is frequently written into the source text without any enclosing braces. Those braces are important because they group the block of code into a single syntactical unit. When you later try to unpick someone else’s script, the indentation may fool you into seeing several lines that appear to be conditionally executed when in fact only one is. The same applies to iterators as well. It is highly recommended that you put in the braces where they are required for multiple line conditional code and iterator blocks even when there is only one line of code being executed. This safeguards against errors when more lines are added to the conditional or iterated code block later on. This is not recommended practice: if(aCondition) someCode; while(aCondition) someCode; for( … ) someCode; This is slightly better but requires more effort when adding lines to the code block: if(aCondition) someCode; while(aCondition) someCode; for( … ) someCode; This is less dangerous than having no braces but makes the line long and twice as hard to scan visually: if(Condition) { someCode } This is fashionable, but the braces are hard to balance visually: if(aCondition) { someCode } 1568

Personal web server - Obfuscation (Advice) Needless complexity in the arrangement of

Friday, February 29th, 2008

Obfuscation (Advice) Needless complexity in the arrangement of tokens in a line of executable script. There are competitions on the Internet to write the most egregious and highly functional code fragment in the fewest lines of code. This is particularly easy to do in C language. Certain operators lend themselves to the construction of extremely terse code, which, although functionally very clever, is also very hard to understand when diagnosing faults or carrying out maintenance. This entry is not to recommend against the use of such operators, but to urge a word of caution on the basis that modern language parsing and execution engines may be smart enough to highly optimize the code, making any performance gains negligible anyway. These days, it is quite difficult to yield any noticeable performance gains by epitomizing the code at the source level with compiled systems. There may still be some gains to be won with interpreted code. A badly designed algorithm may continue to perform badly in an interpreter where there is a possibility it might get corrected in an optimizing compiler. The operators to be particularly cautious about are the assignment operators and the prefix/postfix operators. The ternary conditional operator is also hard to read in source and may offer little advantage over an if( ) else construct. These are equivalent but intent is much more obvious withthe if( ) else form: // Conditional ternary operator myResult = (mySwitch) ? “TRUE VALUE” : “FALSE VALUE” ; // Conditional block if(mySwitch) { myResult = “TRUE VALUE”;} else { myResult = “FALSE VALUE”;}

JavaScript Programmer’s Reference Hexadecimal values must always be

Thursday, February 28th, 2008

JavaScript Programmer’s Reference Hexadecimal values must always be integers, thus: . 0xFF . 0XABCD Octal values must always be integers, must start with a zero and contain only the characters 0 7. The standard does not mandate any particular rounding technique but recommends the use of IEEE 754 standard numeric computation. This standard has been in existence for some time now and is likely to be the foundation numeric computation standard in most platforms. See also: Floating constant, Implicit conversion, Literal, Number formats (.) Cross-references: ECMA 262 edition 2 section 7.7.3 ECMA 262 edition 3 section 7.8.3 Numerical limits (Definition) Limiting conditions for arithmetic values. Refer to: Limits 1566

Web design - N Number.valueOf() (Method) Cross-references: ECMA 262 edition

Wednesday, February 27th, 2008

N Number.valueOf() (Method) Cross-references: ECMA 262 edition 2 section 15.7.4.2 ECMA 262 edition 3 section 15.7.4.2 Number.valueOf() (Method) Return the primitive numeric value of the object. Availability: ECMAScript edition 2 JavaScript 1.1 JScript 3.0 Internet Explorer 4.0 Netscape 3.0 Property/method value type: Number primitive JavaScript syntax: -myNumber.valueOf() A Numberobject is converted to a simple Number primitive. You probably won’t need to do this very often yourself because JavaScript is smart enough to convert Number primitives to Number objects and vice versa whenever it needs to. This is a whole lot better than having to cast data types to get expressions to work, as you would have to in other programming languages. This help makes JavaScript very accessible to non-programmers. See also: Cast operator, Number.prototype, valueOf() Cross-references: ECMA 262 edition 2 section 15.7.4.3 ECMA 262 edition 3 section 15.7.4.4 Numeric literal (Primitive value) A literal constant whose type is a built-in primitive value. Availability: ECMAScript edition 2 Property/method value type: Number primitive Numeric literals are constant numeric values expressed in Decimal, Hexadecimal, or Octal notation. Numeric values can be integer or floating-point. Floating-point values can be specified with exponential notation. 1565

Web hosting contract - JavaScript Programmer’s Reference Example code: // Create a

Tuesday, February 26th, 2008

JavaScript Programmer’s Reference Example code: // Create a number and then examine its source myNumber = new Number(1000); document.write(myNumber.toSource()); Number.toString() (Method) Return a string primitive version of an object. Availability: ECMAScript edition 2 JavaScript 1.1 JScript 1.0 Internet Explorer 3.02 Netscape 3.0 Opera 3.0 Property/method value type: String primitive JavaScript syntax: -myNumber.toString(aRadix) Argument list: aRadix A radix to base the string conversion of the value on The result of this method is a String primitive representation of the numeric value of the receiving object, rendered according to the passed-in radix value. A radix is the number of discrete values in the counting sequence before a carry over digit is generated. Thus, the radix of decimal numbers is 10. With this mechanism, you can generate values to any radix. The radix is also called the base of the number system. The radix value is in the range 2 to 36 and allows the numeric value to be reproduced in a variety of number bases. . A missing radix is assumed to be base 10. . Octal numbers use a radix value of 8. . Hexadecimal numbers use a radix value of 16. . Binary numbers use a radix value of 2. For an example, refer to the Decimal value topic where this is used to generate a lookup table. Before the radix conversion was available, this method simply converted to a string, which most commentators considered was unnecessary since JavaScript did this naturally without any need to add special scripting support. However, now that we can convert numeric values from one base to another, this function becomes far more useful. See also: Cast operator, Decimal value, Hexadecimal value, Number.prototype, Number.toExponential(), Number.toFixed(), Octal value, toString() 1564

Apache web server tutorial - N Number.toPrecision() (Method) Number.toPrecision() (Method) Convert a

Tuesday, February 26th, 2008

N Number.toPrecision() (Method) Number.toPrecision() (Method) Convert a number to a string automatically selecting fixed or exponential notation. The number of digits after the decimal point This method will convert a number to a string and will select either a fixed or exponential notation according to the magnitude of the value being converted. This would be useful where you have an arbitrary collection of values and don’t want them presented in a ragged looking column. Availability: ECMAScript edition 3 JavaScript 1.5 JScript 5.5 Internet Explorer 5.5 Netscape 6.0 Property/method value type: String primitive JavaScript syntax: -myNumber.toPrecision(aNumber) Argument list: aNumber See also: Number.toExponential(), Number.toFixed() Cross-references: ECMA 262 edition 3 section 15.7.4.7 Number.toSource() (Method) Output a number formatted as a Number literal contained in a string. Availability: ECMAScript edition 3 JavaScript 1.3 Netscape 4.06 Property/method value type: String primitive JavaScript syntax: -myNumber.toSource() This is an alternative way to deliver a string version of a number value. In this case, it is formatted as a Number literal and can then be used in an eval() function to assign another number. If you run the example below, it should yield this as a result: (new Number(1000)) The result of calling this method is a string version of the number formatted as a Number literal. 1563

JavaScript Programmer’s Reference This method is useful for

Monday, February 25th, 2008

JavaScript Programmer’s Reference This method is useful for truncate formatting number values. This is especially helpful when presenting tables of financial data that needs to be justified and padded to the same number of digits after the decimal point. The argument value indicates the precision or decimal places of accuracy to the right of the decimal point character. If the argument is undefined, then zero is assumed and the value will be presented as an integer. This method may be useful for performing truncations from floating point to integer value. The output of this method may also be more precise when a large number of integer digits are required to present the number. According to the ECMA standard, the alternative toString() method loses some accuracy for numbers having 19 digits for example. See also: Number.toExponential(), Number.toPrecision(), Number.toString() Cross-references: ECMA 262 edition 3 section 15.7.4.5 Number.toLocaleString() (Method) Converts a number to a string taking locale-specific settings into account. Availability: ECMAScript edition 3 JavaScript 1.5 JScript 5.5 Internet Explorer 5.5 Netscape 6.0 Property/method value type: String primitive JavaScript syntax: -myNumber.toLocaleString() Locale conventions may include special formatting of numbers such as thousands separators and decimal point symbols. The number will be converted according to the rules defined by the implementation’s locale setting. Warnings: . ECMA warns that the first argument of this method is reserved for future use. Beware if your implementation makes use of an argument in this method as it may be non-compliant with a later version of the ECMA standard. Cross-references: ECMA 262 edition 3 section 15.7.4.3 1562

N Number.toExponential() (Method) Cross-references: ECMA 262 edition (Web server type)

Sunday, February 24th, 2008

N Number.toExponential() (Method) Cross-references: ECMA 262 edition 2 section 15.2.3.1 ECMA 262 edition 2 section 15.7.4 ECMA 262 edition 3 section 15.7.3.1 Number.toExponential() (Method) Converts the number to an exponential format representation. Availability: Property/method value type: JavaScript syntax: Argument list: This method is useful for formatting number values. This is especially helpful when presenting tables of scientific data, which may require values of a wide range of magnitudes to be presented in a similar way. The argument value indicates the precision or decimal places of accuracy to the right of the decimal point character. If the argument is undefined, then as many digits as are necessary to completely distinguish the value are presented (up to the mathematical accuracy of the implementation). ECMAScript edition 3 JavaScript 1.5 JScript 5.5 Internet Explorer 5.5 Netscape 6.0 String primitive -myNumber.toExponential(aNumber) aNumber The number of digits after the decimal point See also: Number.toFixed(), Number.toPrecision(), Number.toString() Cross-references: ECMA 262 edition 3 section 15.7.4.6 Number.toFixed() (Method) Converts the number to a fixed format representation. ECMAScript edition 3 JavaScript 1.5 JScript 5.5 Internet Explorer 5.5 Netscape 6.0 String primitive -myNumber.toFixed(aNumber) aNumber The number of digits after the decimal point Availability: Property/method value type: JavaScript syntax: Argument list: 1561

Web site designers - JavaScript Programmer’s Reference Number.prototype (Property) The prototype for

Sunday, February 24th, 2008

JavaScript Programmer’s Reference Number.prototype (Property) The prototype for the Number object that can be used to extend the interface for all Number objects. Availability: ECMAScript edition 2 JavaScript 1.1 JScript 1.0 Internet Explorer 3.02 Netscape 3.0 Netscape Enterprise Server version 2.0 Opera 3.0 Property/method value type: Number object -Number.prototype JavaScript syntax: -myNumber.constructor.prototype The prototype property refers to the built-in Number prototype object. The example demonstrates how to provide extensions to all instances of this class by adding a function to the prototype object. Example code: Number object, Number(), Number(), Number.constructor, Number.toString(), Number.valueOf(), prototype property See also: Property attributes: DontDelete, DontEnum. 1560

N Number.POSITIVE_INFINITY (Constant/static) Number.POSITIVE_INFINITY (Constant/static) (Web hosting reseller) A mathematical

Saturday, February 23rd, 2008

N Number.POSITIVE_INFINITY (Constant/static) Number.POSITIVE_INFINITY (Constant/static) A mathematical constant value. Availability: ECMAScript edition 2 JavaScript 1.1 JScript 1.0 Internet Explorer 3.02 Netscape 3.0 Netscape Enterprise Server 2.0 Opera 3.0 Property/method value type: Number primitive JavaScript syntax: -Number.POSITIVE_INFINITY This is a constant value representing positive infinity. It should be identical to the Infinity value provided as a property of the Global object in an ECMA-compliant implementation. Refer to the Infinity topic for further discussion. Warnings: . Netscape 2.02 does not understand what Number.POSITIVE_INFINITY is. See also: Arithmetic constant, Infinity, Number.constructor, Special number values Property attributes: ReadOnly, DontDelete, DontEnum. Cross-references: ECMA 262 edition 2 section 15.7.3.6 ECMA 262 edition 3 section 15.7.3.6 O’Reilly JavaScript Definitive Guide page 37 Wrox Instant JavaScript page 15 1559