Nytro Posted May 3, 2011 Report Posted May 3, 2011 JavaScript GardenJavaScript Garden is a growing collection of documentation about the most quirky parts of the JavaScript programming language. It gives advice to avoid common mistakes, subtle bugs, as well as performance issues and bad practices that non-expert JavaScript programmers may encounter on their endeavours into the depths of the language.JavaScript Garden does not aim to teach you JavaScript. Former knowledge of the language is strongly recommended in order to understand the topics covered in this guide. In order to learn the basics of the language, please head over to the excellent guide on the Mozilla Developer Network.The AuthorsThis guide is the work of two lovely Stack Overflow users, Ivo Wetzel (Writing) and Zhang Yi Jiang (Design).Contributors Caio Romão (Spelling corrections) Andreas Blixt (Language corrections)HostingJavaScript Garden is hosted on GitHub, but Cramer Development supports us with a mirror at JavaScriptGarden.info.LicenseJavaScript Garden is published under the MIT license and hosted on GitHub. If you find errors or typos please file an issue or a pull request on the repository. You can also find us in the JavaScript room on Stack Overflow chat. ObjectsObject Usage and PropertiesEverything in JavaScript acts like an object, with the only two exceptions being null and undefined.false.toString() // 'false'[1, 2, 3].toString(); // '1,2,3'function Foo(){}Foo.bar = 1;Foo.bar; // 1A common misconception is that number literals cannot be used as objects. That is because a flaw in JavaScript's parser tries to parse the dot notation on a number as a floating point literal.2.toString(); // raises SyntaxErrorThere are a couple of workarounds which can be used in order make number literals act as objects too.2..toString(); // the second point is correctly recognized2 .toString(); // note the space left to the dot(2).toString(); // 2 is evaluated firstObjects as a Data TypeObjects in JavaScript can also be used as a Hashmap, they mainly consist of named properties mapping to values.Using a object literal - {} notation - it is possible to create a plain object. This new object inherits from Object.prototype and has no own properties defined on it.var foo = {}; // a new empty object// a new object with a property called 'test' with value 12var bar = {test: 12}; Accessing PropertiesThe properties of an object can be accessed in two ways, via either the dot notation, or the square bracket notation.var foo = {name: 'Kitten'}foo.name; // kittenfoo['name']; // kittenvar get = 'name';foo[get]; // kittenfoo.1234; // SyntaxErrorfoo['1234']; // worksBoth notations are identical in their workings, with the only difference being that the square bracket notation allows for dynamic setting of properties, as well as the use of property names that would otherwise lead to a syntax error.Deleting PropertiesThe only way to actually remove a property from an object is to use the delete operator; setting the property to undefined or null only remove the value associated with the property, but not the key.var obj = { bar: 1, foo: 2, baz: 3};obj.bar = undefined;obj.foo = null;delete obj.baz;for(var i in obj) { if (obj.hasOwnProperty(i)) { console.log(i, '' + obj[i]); }}The above outputs both bar undefined and foo null - only baz was removed and is therefore missing from the output.Notation of Keysvar test = { 'case': 'I am a keyword so I must be notated as a string', delete: 'I am a keyword too so me' // raises SyntaxError};Object properties can be both notated as plain characters and as strings. Due to another mis-design in JavaScript's parser, the above will throw a SyntaxError prior to ECMAScript 5.This error arises from the fact that delete is a keyword; therefore, it must be notated as a string literal to ensure that it will be correctly interpreted by older JavaScript engines.Articol frumos aranjat aici:http://bonsaiden.github.com/JavaScript-Garden/ Quote
tdxev Posted May 4, 2011 Report Posted May 4, 2011 sunt acolo cateva lectii destul de interesante pentru cine vrea sa invete jsjavascript.crockford.com Quote