Version 1.7 glow.lang
API Quick Reference
JavaScript is required to use the quick reference
Useful language functions.
Further Info & Examples
Methods
- apply
-
Copies properties from one object to another
Synopsis
glow.lang.apply(destination, source);
Parameters
- destination
-
- Type
- Object
Destination object
- source
-
- Type
- Object
Properties of this object will be copied onto the destination
Returns
Example
var obj = glow.lang.apply({foo: "hello", bar: "world"}, {bar: "everyone"}); //results in {foo: "hello", bar: "everyone"}
- clone
-
Deep clones an object / array
- extend
-
Copies the prototype of one object to another.
Synopsis
glow.lang.extend(sub, base, additionalProperties);
Parameters
- sub
-
- Type
- Function
Class which inherits properties.
- base
-
- Type
- Function
Class to inherit from.
- additionalProperties
-
- Type
- Object
An object of properties and methods to add to the subclass.
Description
The 'subclass' can also access the 'base class' via subclass.base
Example
function MyClass(arg) { this.prop = arg; } MyClass.prototype = { showProp: function() { alert(this.prop); } }; function MyOtherClass(arg) { //call the base class's constructor arguments.callee.base.apply(this, arguments); } glow.lang.extend(MyOtherClass, MyClass, { setProp: function(newProp) { this.prop = newProp; } }); var test = new MyOtherClass("hello"); test.showProp(); // alerts "hello" test.setProp("world"); test.showProp(); // alerts "world"
- interpolate
-
Replaces placeholders in a string with data from an object
Synopsis
glow.lang.interpolate(template, data, opts);
Parameters
- template
-
- Type
- String
The string containing {placeholders}
- data
-
- Type
- Object
Object containing the data to be merged in to the template
The object can contain nested data objects and arrays, with nested object properties and array elements are accessed using dot notation. eg foo.bar or foo.0.
The data labels in the object cannot contain characters used in the template delimiters, so if the data must be allowed to contain the default { and } delimiters, the delimters must be changed using the option below.
- opts
-
- Type
- Object
Options object
- delimiter
-
Alternative label delimiter(s) for the template
- Type
- String
- Default
- "{}"
- Optional
- Yes
The first character supplied will be the opening delimiter, and the second the closing. If only one character is supplied, it will be used for both ends.
- escapeHtml
-
Escape any special html characters found in the data object
- Type
- Boolean
- Default
- false
- Optional
- Yes
Use this to safely inject data from the user into an HTML template. The glow.dom module must be present for this feature to work (an error will be thrown otherwise).
Returns
Example
var data = { name: "Domino", colours: ["black", "white"], family: { mum: "Spot", dad: "Patch", siblings: [] } }; var template = "My cat's name is {name}. His colours are {colours.0} & {colours.1}. His mum is {family.mum}, his dad is {family.dad} and he has {family.siblings.length} brothers or sisters."; var result = glow.lang.interpolate(template, data); // result == "My cat's name is Domino. His colours are black & white. His mum is Spot, his dad is Patch and he has 0 brothers or sisters."
var data = { name: 'Haxors!!1 <script src="hackhackhack.js"></script>' } var template = '<p>Hello, my name is {name}</p>'; var result = glow.lang.interpolate(template, data, { escapeHtml: true }); // result == '<p>Hello, my name is Haxors!!1 <script src="hackhackhack.js"></script></p>'
- map
-
Runs a function for each element of an array and returns an array of the results
Synopsis
glow.lang.map(array, callback, context);
Parameters
- array
-
- Type
- Array
Array to loop over
- callback
-
- Type
- Function
The function to run on each element. This function is passed three params, the array item, its index and the source array.
- context
-
- Type
- Object
- Optional
- Yes
The context for the callback function (the array is used if not specified)
Returns
Array containing one element for each value returned from the callback
Example
var weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"] var weekdaysAbbr = glow.lang.map(weekdays, function (day) { return day.slice(0, 3).toLowerCase(); }); // returns ["mon", "tue", "wed", "thu", "fri"]
- replace
-
Makes a replacement in a string.
Synopsis
glow.lang.replace(str, pattern, replacement);
Parameters
- str
-
- Type
- String
Input string
- pattern
-
- Type
- String | RegExp
String or regular expression to match against
- replacement
-
String to make replacements with, or a function to generate the replacements
Returns
A new string with the replacement(s) made
Description
Has the same interface as the builtin String.prototype.replace method, but takes the input string as the first parameter. In general the native string method should be used unless you need to pass a function as the second parameter, as this method will work accross our supported browsers.
Example
var myDays = '1 3 6'; var dayNames = glow.lang.replace(myDays, /(\d)/, function (day) { return " MTWTFSS".charAt(day - 1); }); // dayNames now contains "M W S"
- toArray
-
Converts an array-like object to a real array
- trim
-
Removes leading and trailing whitespace from a string