Skip to main content

Home/ javascript/ Group items tagged string

Rss Feed Group items tagged

Javier Neira

Google Closure: How not to write JavaScript - 3 views

  • “It’s a JavaScript library written by Java developers who clearly don’t get JavaScript.”
  • Unfortunately, unlike the built-in properties supplied by Object.prototype, custom properties added to Object.prototype will show up as an object property in any for-in loop in the page.
  • custom properties added to
  • ...3 more annotations...
  • will show up as an object property in any
  • loop in the page.
  • var a = "I am a string!";   2 alert(typeof a); // Will output "string"   3 var b = new String("I am also a string!");   4 alert(typeof b); // Will output "object" 
Javier Neira

6 Advanced JavaScript Techniques You Should Know - 3 views

  • function showStatistics(args) { document.write("<p><strong>Name:</strong> " + args.name + "<br />"); document.write("<strong>Team:</strong> " + args.team + "<br />"); if (typeof args.position === "string") { document.write("<strong>Position:</strong> " + args.position + "<br />"); } if (typeof args.average === "number") { document.write("<strong>Average:</strong> " + args.average + "<br />"); } if (typeof args.homeruns === "number") { document.write("<strong>Home Runs:</strong> " + args.homeruns + "<br />"); } if (typeof args.rbi === "number") { document.write("<strong>Runs Batted In:</strong> " + args.rbi + "</p>"); } } showStatistics({ name: "Mark Teixeira" }); showStatistics({ name: "Mark Teixeira", team: "New York Yankees" }); showStatistics({ name: "Mark Teixeira", team: "New York Yankees", position: "1st Base", average: .284, homeruns: 32, rbi: 101 });
  • Object-oriented JavaScript implements namespace-like principles due to the fact that properties and methods are declared inside of objects, thus there are less likely to be conflicts. A conflict could arise, however, through object names. And very likely, the conflict will occur "silently", thus you may not be alerted to the issue immediately.
  • if (typeof MY == "undefined") { MY = new Object(); MY.CUSTOM = new Object(); } MY.CUSTOM.namespace = function() { function showStatistics(args) { document.write("<p><strong>Name:</strong> " + args.name + "<br />"); document.write("<strong>Team:</strong> " + args.team + "<br />"); if (typeof args.position === "string") { document.write("<strong>Position:</strong> " + args.position + "<br />"); } if (typeof args.average === "number") { document.write("<strong>Average:</strong> " + args.average + "<br />"); } if (typeof args.homeruns === "number") { document.write("<strong>Home Runs:</strong> " + args.homeruns + "<br />"); } if (typeof args.rbi === "number") { document.write("<strong>Runs Batted In:</strong> " + args.rbi + "</p>"); } } showStatistics({ name: "Mark Teixeira", team: "New York Yankees", position: "1st Base", average: .284, homeruns: 32, rbi: 101 }); } MY.CUSTOM.namespace();
  • ...1 more annotation...
  • Rendering Readable HTML
  •  
    //2. Object Literals to Pass Optional Arguments function showStatistics(args) { document.write("Name: " + args.name + "
    "); document.write("Team: " + args.team + "
    "); if (typeof args.position === "string") { document.write("Position: " + args.position + "
    "); } if (typeof args.average === "number") { document.write("Average: " + args.average + "
    "); } if (typeof args.homeruns === "number") { document.write("Home Runs: " + args.homeruns + "
    "); } if (typeof args.rbi === "number") { document.write("Runs Batted In: " + args.rbi + ""); } } showStatistics({ name: "Mark Teixeira" }); showStatistics({ name: "Mark Teixeira", team: "New York Yankees" }); showStatistics({ name: "Mark Teixeira", team: "New York Yankees", position: "1st Base", average: .284, homeruns: 32, rbi: 101 }); //Using Namespaces to Prevent Conflicts if (typeof MY == "undefined") { MY = new Object(); MY.CUSTOM = new Object(); } MY.CUSTOM.namespace = function() { function showStatistics(args) { .................. } showStatistics({ name: "Mark Teixeira", team: "New York Yankees", position: "1st Base", average: .284, homeruns: 32, rbi: 101 }); } MY.CUSTOM.namespace();
yc c

Regexp Syntax Summary - 0 views

  •  
    This table summarizes the meaning of various strings in different regexp syntaxes. It is intended as a quick reference, rather than a tutorial or specification. Please report any errors.
Javier Neira

All about types in Javascript - The basics | united-coders.com - 0 views

  • This may lead to interesting results. When dealing with arrays, an array is converted to a string by converting the elements to string, seperating each element by a comma. [].toString();      // "" [2].toString();     // "2" [1,2,3].toString(); // "1,2,3"
Javier Neira

Ajaxian » ECMAScript Edition 5: WebKit JavaScriptCore Progress - 0 views

  •  
    * Object.getPrototypeOf * Object.getOwnPropertyDescriptor * Object.keys * Object.defineProperty * Object.defineProperties * Object.create * Object.getOwnPropertyNames * JSON.stringify * JSON.parse * Date.now: Has been in JSC for a long time. * Date.prototype.toISOString * Date.prototype.toJSON * String.prototype.trim * "Array extras": Array.prototype.{indexOf,lastIndexOf,every,some,forEach,map,filter,reduce,reduce
Javier Neira

7 + 8 === 7 in JavaScript » Debuggable Ltd - 0 views

  • JavaScript assumes that any 0-prefixed string ought to be referring to an octal number. This will not become apparent until your string represents an invalid octal such as '08'. So in order to outsmart this "feature" you have to explicitly provide the base for your integer:
Javier Neira

JavaScript setTimeout Function - JavaScript Timing Events - 0 views

  •  
    JavaScript setTimeout Function - JavaScript Timing Events November 16, 2007 by Blogging Developer JavaScript features a couple of methods that lets you run a piece of JavaScript code (javascript function) at some point in the future. These methods are: * setTimeout() * setInterval() In this tutorial, I'll explain how setTimetout() method works, and give a real world example. You may find the details of setInterval() method in JavaScript setInterval Function - JavaScript Timing Events setTimeout() window.setTimeout() method allows you to specify a piece of JavaScript code (expression) will be run after specified number of miliseconds from when the setTimeout() method is called. Syntax var t = setTimeout ( expression, timeout ); The setTimeout() method returns a numeric timeout ID which can be used to refer the timeout to use with clearTimeout method. The first parameter (expression) of setTimeout() is a string containing a javascript statement. The statement could be a call to a JavaScript function like "delayedAlert();" or a statement like "alert('This alert is delayed.');". The second parameter (timeout), indicates the number of miliseconds to pass before executing the expression. Example An alert box will be shown 5 seconds later when you clicked the button. clearTimeout() Sometimes it's useful to be able to cancel a timer before it goes off. The clearTimeout() method lets us do exactly that. Its syntax is: clearTimeout ( timeoutId ); where timeoutId is the ID of the timeout as returned from the setTimeout() method call.
Javier Neira

JavaScript EE, Part 1: Run JavaScript files on the server side - 0 views

  • freedom to use the same JavaScript routines on both servers and clients. In addition, the techniques presented throughout this series will allow you to maintain a single code base for both Ajax and non-Ajax clients
  • This double-coding issue can be avoided by using JavaScript combined with Java code on the server side, getting full support of scripting languages through the javax.script API. In addition, the Java SE Development Kit (JDK) 6 already includes Mozilla's Rhino JavaScript engine, which means no setup is required.
  • the toSource() method, which all JavaScript objects must have.
  • ...5 more annotations...
  • engine.eval(scriptReader, vars);
  • sun.org.mozilla.javascript.internal.NativeObject
  • all data exchange between the Java code and the executed script should be done through primitive variables, strings, and Java objects (for example, beans) whose properties and methods can be accessed very easily in the JavaScript code. Simply said, don't try to access native JavaScript objects in your Java code. Use Java objects in the JavaScript code instead.
  • Note that javax.script.Invocable is an optional interface, which some script engines may not implement. The JavaScript engine that comes with JDK 6 does support this interface.
  • you can significantly reduce the execution time by compiling the scripts, using the methods provided by another optional interface named javax.script.Compilable, which is supported by the JavaScript engine of JDK 6.
Javier Neira

InfoQ: ECMAScript 5 released - 1 views

  • The introduction of strict mode aims to avoid common coding problems in ECMAScript applications. This is achieved with the presence of a lone string literal in a unit (script or function): "use strict;"
  • for either the entire script (if at the top of the script) or for a single function (if the first part of a function).
  • var i=3 is needed
  • ...10 more annotations...
  • and introducing new variables through eval cannot occu
  • delete cannot be used against arguments, functions or variables or other properties with the configurable flag set to false
  • with statements, often a source of errors, are no longer used and considered syntax errors
  • Functions can no longer have duplicate arguments with the same name Objects can no longer have duplicate properties with the same name
  • Access to the global object becomes a runtime error
  • A new JSON object with parse and stringify to support efficient generation of JSON data; like eval but without the security implications of being able to reduce code
  • Array now has standard functions, such as indexOf(), map(), filter(), and reduce()
  • Object now has seal()
  • and freeze()
  • Object.getPrototypeof() returns the prototype of the given object
1 - 12 of 12
Showing 20 items per page