Trucs et astuces en javascript v1.8 (SpiderMonkey/TraceMonkey)
08/06/2009
Avec l'arrivée du nouveau moteur javascript SpiderMonkey/TraceMonkey sur Firefox 3.5, de nouvelles possibilités s'offrent à vous. Voici donc une énorme liste de trucs et astuces qui utilisent pour la plupart des fonctions de ce nouveau moteur.
Certaines peuvent fonctionner avec des versions antérieurs (précisé sous le titre si c'est le cas)
C'est aux développeurs de jslibs que nous devons cette liste. Je vous en met 2-3 extraits ici, suivi de la liste complète.
//Texte multiligne
var text = <>
this
is
my
multi-line
text
.toString();
Print(text);
var text = <>
this
is
my
multi-line
text
.toString();
Print(text);
//JavaScript code beautifier (un-minifier)
function foo() {var a;var b=[1,2,3];var c={x:{y:1}};function bar(){return 1}}
Print( foo.toSource(1) );
function foo() {var a;var b=[1,2,3];var c={x:{y:1}};function bar(){return 1}}
Print( foo.toSource(1) );
//la sortie :
function foo() {
var a;
var b = [1, 2, 3];
var c = {x: {y: 1}};
function bar() {
return 1;
}
}
//Multiple-value returns
function f() {
function f() {
return [1, 2];
}
var [a, b] = f();
Print( a + ' ' + b ); // prints: 1 2
La liste complète
- language advanced Tips & Tricks
- shuffle the Array
- multi-line text
- Escape and unescape HTML entities
- Remove an object from an array
- Creates a random alphabetic string
- Brainfuck interpreter
- Optional named function arguments
- Floating to integer
- String converter
- Display the current call stack
- Get the number of properties of an object
- Check if an object is not empty
- Change the primitive value of an object with valueOf
- Transform the arguments object into an array
- Convert a string into a charcode list
- Array iteration pitfall
- exceptions for non-fatal errors
- A Switch function
- A Match function
- new object override
- Kind of destructuring assignments
- Generator Expressions
- Advanced use of iterators
- Expression Closures
- Function declaration and expression
- Factory method pattern
- Closures by example
- Minifier / comment remover
- code beautifier (un-minifier)
- Auto indent code / unobfuscator
- Objects, constructor and instanceof
- Objects private and public members
- Stack data structure
- Singleton pattern
- Use Functions as an Object
- E4X add nodes
- E4X dynamic document creation
- E4X dynamic tag name
- E4X dynamic content
- E4X iteration
- Listen a property for changes
- Logical operators tricks
- Functions argument default value
- Remove an item by value in an Array object
- Multiple-value returns
- Operator [ ] and strings ( like charAt() )
- indexOf() and lastIndexOf() Works on Array
- Using Array functions on a non-Array object
- Simulate threads using yield operator
- Change current object (this) of a function call
- Filter / intercept a function call
- E4X Query ( like XPath )
- swap two variables
- Destructuring assignment with function arguments
- scope is not C/C++ scope
- scope and LET instruction
- Defer function calls
- Insert an array in another array
- Multiple string concatenation
- HTTP headers parser
- Using 'with' scope
- (object).toString()
- .$1
- Binary with XmlHTTPRequest
- Iterate on values
- Exceptions Handling / conditional catch (try catch if)
- Special chars
- object's eval method
- eval this
- No Such Method ( noSuchMethod )
- replace
- Values comparison
- undefined, null, 0, false, '', ...
- constructor property ( + Type )
- AJAX evaluation
- Comma operator
- closures pitfall
- sharp variable
- common object between 2 objects
- constructor
- string can contain null chars
- 'new' operator precedence
- constructor usage
- To object
- Serialize or uneval a variable or an object ( can be nested )
- labels
- for in loop
- proto ( prototype property )
- Numbers and floating point error
- Number base conversion ( hexadecimal )
- try / catch / finally
- Object argument
- object and its prototype
- Runtime prototype object
- for in loop and undefined value
- Call function in parent class
- getter and setter function
- defineGetter ( define getter )
- check if an object or its prototype has a propery (hasOwnProperty)
- check if a property is enumerable (propertyIsEnumerable)
- find the getter/setter function of an object ( lookup getter )
- suppress array element while iterating it
- arrays
- delete array element
- Site internet : http://code.google.com/p/jslibs/wiki/JavascriptTips
Commentaires récents