background preloader

Javascript 1

Facebook Twitter

Jquery

Doris. Interface. Memoization. Kvstore. JSMIPS. WPS: PostScript for the Web. JavaScript Hashtable. The code is contained in one JavaScript file, which creates a single constructor function called Hashtable in the global scope. Doesn't JavaScript already do this? No. Although a JavaScript object can be used as a hash, there are several limitations that make using a JavaScript object unsuitable for a generic hash. One limitation is that only strings and numbers tend to make useful keys. For example, the following is simple and works: var key = "A key"; var o = {}; o[key] = 1; alert( o[key] ); // Alerts 1 However, it is often desirable to use other kinds of objects as keys. Var key = {}; var o = {}; o[key] = "First"; alert( o[key] ); // Alerts "First" It doesn't take much effort to discover the flaw in this approach: var key1 = {}; var key2 = {}; var o = {}; o[key1] = "First"; o[key2] = "Second"; alert( o[key1] ); // Alerts "Second", not "First" With jshashtable, any JavaScript value apart from null and undefined can be used as a key : Equality Why do we get null?

Hash codes Top. JavaScript Reusability Is A Quagmire. WebOS from O&#039;Reilly Media. Robert Nyman on the Javascript Language. Friday, November 20th, 2009 <>p>Robert Nyman walks through some of the more subtle low-level features of Javascript, and some of the idioms that have emerged. Comparisons: Understanding identity (===) versus equality (==). Boolean expressions: Understanding how short-circuit logic (if a && b won’t eval b if a is false); Types: Type coercion (“1″+2+3); “falsey” (false, null, 0) versus “truthy”; the importance of using operators like parseInt and instanceof. Functions: Anonymous functions; self-invoking functions – function() { })() ; using the arguments collection to get all arguments to the current function, important to note it’s not a real array with all the array methods, and using arguments to overload arguments.

Inheritance: Using the prototype chain for inheritance from subclass to superclasses up to Object. Global scope: Avoid using global scope where you can. Sugaring: Adding syntax sugar, e.g. extending String.prototype. Javascript Inheritance Patterns: Learning from Closure. Inheritance Patterns in JavaScript. Inheritance Patterns in JavaScript The Closure Library makes use of the pseudoclassical inheritance pattern, which is particularly compelling when used with the Closure Compiler. Those of you who have read JavaScript: The Good Parts by Douglas Crockford may use the functional pattern for inheritance that he espouses.

Crockford appears to object to the pseudoclassical pattern because: "There is no privacy; all properties are public. There is no access to super methods...Even worse, there is a serious hazard with the use of constructor functions. If you forget to use the new prefix when calling a constructor function, then this will not be bound to a new object...There is no compile warning, and there is no runtime warning. " This article discusses the advantages of the pseudoclassical pattern over the functional pattern. Example of the functional pattern var phone = function(spec) { var that = {}; that.getPhoneNumber = function() { return spec.phoneNumber; }; return that;}; Not really. A JavaScript War Story. What follows is an account from the author’s experience. Some details have been changed for the usual reasons, and a poor memory has fuzzed out the rest. UPDATE: it turns out the technique described below is known as debouncing.

What an awesome name! My last job was for a company that made case-management software. One of our customers’ favorite features was the reports, and they were pretty spiffin’. The javascript was pretty involved. No biggie, I thought, and I quickly added window.onResize(reportPreview);. I knew we could delay calling reportPreview, but we only wanted to delay it when the window was being resized — when the user changed the columns, there was no reason to wait. I’d been reading The Little Schemer lately, and noticing some striking similarities between javascript and scheme: first-class functions, and higher-order functions that take functions as arguments, and return other functions as values.

Then I realized higher-order functions were a way out of this. Like this: 6 Advanced JavaScript Techniques You Should Know. By Louis Lazaris There have been a number of articles published over the years that discuss best practices techniques for JavaScript. I thought I would go a little bit beyond the scope of those articles and outline a number of advanced techniques and practices that I have personally used or read about that could be invaluable in certain circumstances. This article doesn’t necessarily cover every detail of the methods I’m describing, but provides an overview, along with code examples, of some practical JavaScript coding techniques. 1.

Closures to Extend Variable Scope Closures in JavaScript are a fairly straightforward concept, and have been discussed online in a number of in-depth articles. Simply put, closures allow variable scope to be extended past the common scope restrictions of functions. "Think of closures as a kind of regional scope: broader than local but not as broad as global. " To create a closure, you nest a function inside of a function. Further Reading 2. 3. Further Reading: 4.

JavaScript Programming Patterns. Who's voting this up? Did you actually read this drivel? The second solution is dealing with the creation of a singleton, which means creating an object and assigning values (e.g. functions) to its properties. JavaScript is object-based rather than object-oriented. In JavaScript, you just need to call the constructor function of your (custom) object with respective parameters to create an object.

You just have objects with properties and methods that you create by calling functions with parameters. This is not object-oriented due to some crazy reason I'm going to make up right now so that JavaScript seems edgy and cool. It's not a stupid language, man, you just don't get it! As stated above, this pattern is not that handy here, but consider a situation when there is some heavy and complex computing involved. Yeah that's not a thunk, it's a Design Pattern. Actually, reading this again now, it's pretty hilarious. An Introduction to JavaScript’s “this” Essential Unobtrusive Javascript Effects That Will Improve Your. There are some effects that need a lot of coding to accomplish but there are some more that need you just to include a javascript file.

This is a small list of cool things you can do with just a javascript file inclusion. We are not talking about including jQuery, prototype and such, only standalone scripts. Embeded media player The Yahoo! Media Player enhances your web site or blog by creating an embedded player for each audio link. Link to the audio filesCopy and paste this code into your page This play button will appear next to each of your audio links: And when you click on it the Yahoo! Adding cool effects on the fly Do you want to add css3 capabilities, glossy images, image zooming and more to your site? Some of the greatest effects that require you to set only some html classes in your markup are: TripleB Allows you to simulate CSS 3 support for border-radius, box-shadow and background-gradient on your webpages.

Curl So this image: can look like this: Edge Reflex Glossy Sphere ShiftZoom. The Summer of Goto. Named function expressions demystified. Surprisingly, a topic of named function expressions doesn’t seem to be covered well enough on the web. This is probably why there are so many misconceptions floating around. In this article, I’ll try to summarize both — theoretical and practical aspects of these wonderful Javascript constructs; the good, bad and ugly parts of them. In a nutshell, named function expressions are useful for one thing only — descriptive function names in debuggers and profilers . Well, there is also a possibility of using function names for recursion, but you will soon see that this is often impractical nowadays. If you don’t care about debugging experience, you have nothing to worry about.

I’ll start with a general explanation of what function expressions are how modern debuggers handle them. One of the two most common ways to create a function object in ECMAScript is by means of either Function Expression or Function Declaration . There’s a subtle difference in behavior of declarations and expressions. Rise of Javascript. Since its birth in 1994, Javascript has come a long way. Today it is one of the most popular programming languages on the web. Javascript has been on the rise and is growing faster since AJAX based applications reignited professional developer interest. It is now possible to it to write applications on the server, the mobile devices, on the browsers (add ons and plugins), inside PDF documents, even in some remote controls and many more to come. Javascript application development in Mobile devices Build Native Applications using Javascript Palm’s new OS is the first mobile platform to be built from the ground up to combine standard technology, innovation and integration.

At its core, webOS leverages several industry-standard technologies, including web technologies such as CSS, XHTML and JavaScript. Javascript can be used on the iphone too. Desktop Application development using Javascript Desktop App using Javascript Server side Scripts using Javascript Javascript in other places. JUnify ― JavaScript Unification Library. JUnify ― JavaScript Unification Library JUnify is a JavaScript library for performing unification on objects and arrays.

It works on both the browser and Node.js. Unification is an algorithm to determine the substitutions needed to make two expressions match. If the expressions contain variables, these will need to be bound to values in order for the match to succeed. If two expressions are not identical or the variables can not be bound, the match fails. The syntax and use-case in this example is similar to destructuring assignment in JavaScript 1.7.

Installation Use npm to install junify: > npm install junify You can then require('junify') and get access to the API explained below. All code in the library is contained in the junify package. Unify(pattern1, pattern2) Unifies pattern1 with pattern2. Variable(name, type) Creates a new named variable with an optionally specified type. Both the unify and variable methods were demonstrated in the introduction. Visit_pattern(pattern, visitor) Are the new JavaScript engines going to completely change the ga. Last week, a gentleman wrote on his blog about the performance of CPython, Jython and IronPython in creating an empty binary tree to demonstrate a performance problem in IronPython. The article was posted to Reddit, but I thought that the demonstration of other how dynamically typed languages performed was more interesting than the comparison of the different Python implementations.

Among the implementations submitted were Slava Pestov’s Factor version and my own version in Clojure. On my home PC, creating a binary tree of depth 22 took 549 seconds in Python, 14 seconds in Clojure and 4 seconds in Factor. But my biggest surprise was when I tried a JavaScript implementation with Google’s V8 engine: it executed in 1.8 seconds. That is 300 times faster than the Python implementation. I have not (yet) done any serious benchmarking of V8, but I think it’s not completely unreasonable to expect that its performance is better than Python’s in most cases. Like this: Like Loading... Is Javascript the New Assembly? When I first started building websites I learned Javascript and the DOM and that's what I programmed with for client-side browser functionality. When AJAX began to be more broadly known I wrapped my own functions around the XMLHttpRequest object and programmed AJAX functionality.

When I began reading more in depth about Javascript, such as reading Douglas Crockford's articles and watching his videos, I became more interested in Javascript and wanted to programming in it more. This is about the time that various Javascript libraries began to become popular and I began to feel some pressure about using them. These new libraries conflicted with my interest in Javascript because I wanted to write in pure Javascript because I liked it.

Javascript is so powerful and flexible that it seemed that these libraries could mutate the DOM and Javascript into something different. They became their own distinct things made up from the DOM and Javascript. Two techniques for faster JavaScript. I like things that go fast, and that includes code that runs fast. With JavaScript (and Java, too), that can be a challenge. So much the better, though. I like challenges too. When someone asks me what's the single best way to speed up "a slow script," naturally I want to know what the script is spending most of its time doing. In browser scripting, it's typical that a "slow" script operation either involves tedious string parsing of some kind, or DOM operations. That's if you don't count programmer-insanity sorts of things, like creating a regular expression object over and over again in a loop.

The two most important pieces of advice I can give on speeding up browser scripts, then, are: 1. No. 1 means don't do things like crawl a big long string using indexOf( ) to tokenize-as-you-go. There are other techniques for avoiding big speedups, but they're more situational. JavaScript for hackers. By garethheyes Introduction I love to use JavaScript in unexpected ways, to create code that looks like it shouldn't work but does, or produces some unexpected behavior. This may sound trivial, but the results I've found lead to some very useful techniques. Each of the techniques described can be used for XSS filter evasion, which was my original intention when developing them. However, learning such JavaScript can dramatically increase your knowledge of the language, helping you become better at cleaning up input, and increase web application security.

So read on and enjoy my weird and wonderful JavaScript hacks. RegExp replace can execute code When using regular expressions with replace the second argument supports a function assignment. 'XSS'.replace(/XSS/g,alert) This results in alert('XSS'); this works because the match from the RegExp is passed to the alert function as an argument. 'somestring'.replace(/some/,function($1){ .replace(/.+/,eval) Page output: Input: Resulting output: The Javascript trap I willingly step into. A response to Richard Stallman’s The Javascript trap We used to live in the world where software was the essential value in computing. It would take years for companies to develop software, which then would be sold. Users would buy software, install it and run it.

This is the world that Richard Stallman decided to change. Richard Stallman won this battle. However, in recent years, value has shifted from software to services. Since it is easier to get lots of good software, we tend to do more with our computers. That’s why these days we see more and more online applications. Many people don’t really care if they are able to modify the Javascript that is being shipped to their browser and run there. When considering Richard Stallman’s point of view, you should think about where to draw the line. I have nothing against the conventions proposed by Stallman for free Javascript programs (although I am not a fan of GPLv3 and I do have reservations about all versions of the GPL).

Code Karma. Generic Functions and JavaScript. New in JavaScript 1.7 - MDC. Cobra: A Little JavaScript Class Library. Today&#039;s JavaScript kookiness. JS Coverflow. Higher-Order JavaScript. PersistJS: Cross Browser Client-Side Persistent Storage Without. Guide to Unobtrusive JavaScript - Part 1. Douglas Crockford: &quot;JavaScript - The Good Parts&quot; Give JavaScript a chance. Java&#039;scrypt. Javascript will be the Next Big Language. How To Make a Bookmarklet For Your Web Application. The Power of Javascript. Jquery Event Delegation Made Easy. Txt2re. Creating Comet Applications In 3 Minutes. Resig on Simple JavaScript Inheritance.