object vs closure

Consider this JavaScript function: 1 2 3 4 5 function fn() { var a = 10; var b = { name: ‘foo’, n: 10 }; } This might produce the following: I say “might” because specific behaviors depend heavily on implementation. This post takes a V8-centric approach with many diagram shapes linking to relevant source…

that=this in javascript

Watch Out: Callbacks And Closures Sometimes Do Not Play Nicely  Of course there are always gotchas with JavaScript. Lets define a callback function: 1 2 3 4 5 6 7 8 9 10 11 var createCallBack = function() { //First function return new function() { //Second function this.message = “Hello World”; return function() {…

reduce function for array in javascript

Parameters callback Function to execute on each value in the array, taking four arguments: previousValue The value previously returned in the last invocation of the callback, or initialValue, if supplied. (See below.) currentValue The current element being processed in the array. index The index of the current element being processed in the array. array The…

delete in js

The delete operator removes a property from an object. delete is only effective on an object’s properties. It has no effect on variable or function names. If the object inherits a property from a prototype, and doesn’t have the property itself, the property can’t be deleted by referencing the object. You can, however, delete it…

javascript IE memory leak

An unfortunate side effect of closures is that they make it trivially easy to leak memory in Internet Explorer. JavaScript is a garbage collected language — objects are allocated memory upon their creation and that memory is reclaimed by the browser when no references to an object remain. Objects provided by the host environment are…

javascript array in depth

What is an Array in JavaScript? A numerically indexed map of values. Traditionally an array reserves a continuous allocation of memory of predefined length. In JavaScript this is not the case. A JavaScript array is simply a glorified object with a unique constructor and literal syntax and an additional set of properties and methods inherited…

add InterpretedFunction to the java scriptEngine using eval

RhinoScriptEngine This is the typical engine we use for js evaluation. http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/com/sun/script/javascript/RhinoScriptEngine.java without ScriptContext When you get back the reference to a function, you need to ask the engine to execute that function for you. And although not pretty, asking js to eval() it for you with a specific set of bindings will actually do the…

access struts form in javascript

if you have <html:form action=”/someAction”> and in your struts-config.xml, you’ve specified a name for that action (which points to an ActionForm) and your html declaration is <html:html xhtml=”true”> and inside your form you have <html:input> (with name of “userName”), then you can do this in your javascript: ( the rendered form will have an attribute…

javascript apply, call and constuctor

Let’s first create a simple function that we will be using through the rest of this post. This function will just return an array with the current value ofthis and the two supplied arguments. <script type=“text/javascript”> function makeArray(arg1, arg2){     return [ this, arg1, arg2 ]; } </script> Most common way, unfortunately, global function calls When we are learning JavaScript we learn how to define…

javascrip closure

First, a very very good article explaining CLOSURE Second, I like this answer in Stackoverflow: A function in JavaScript is not just a reference to a set of instructions (as in C language) but also includes a hidden data structure which is composed of references to all nonlocal variables it uses (captured variables). Such two-piece…

iframe and javascript

Introduction to Iframes Iframes, or inline frames, allow you to load separate html files into an existing document. Iframes can be placed anywhere in the document flow. CSS and JavaScript can be used to manipulate properties of the iframe, such as its position and size. JavaScript can also be used to pass data back and…

chrome window.print() window.close()

Chrome used to open a seperate window containing just the element i wanted printed and its relative css. That window then spawned the .print command which opened another window containing print preview. Now Chrome no longer opens a new window for their print preview. Instead it opens a “dialog with overlay” in the current page…