Please turn on JavaScript. To find out how to do this visit the WebWise JavaScript guide.

Useful language functions.

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 a clone of destination

Returns

Example

var obj = glow.lang.apply({foo: "hello", bar: "world"}, {bar: "everyone"});
//results in {foo: "hello", bar: "everyone"}
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"
hasOwnProperty

Cross-browser implementation

Synopsis

glow.lang.hasOwnProperty(obj, property);

Parameters

obj
Type
Object

The object to check

property
Type
String

Property name

Returns

Boolean

Returns false if a property doesn't exist in an object, or it was inherited from the object's prototype. Otherwise, returns true

Description

Safari 1.3 doesn't support Object.hasOwnProperty , use this method instead.

interpolate

Replaces placeholders in a string with data from an object

Synopsis

glow.lang.interpolate(template, data);

Parameters

template
Type
String

The string containing {placeholders}

data
Type
Object

Object where the key is the placeholder name

Returns

Example

var data = {name: "Domino", colours: "black & white"};
var template = "My cat's name is {name}. His colours are {colours}";
var result = glow.lang.interpolate(template, data);
//result == "My cat's name is Domino. His colours are black & white"
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

context
Type
Object
Optional
Yes

The context for the callback function (the array is used if not specified)

Returns

Array

Array containing one element for each value returned from the callback

Example

var days = glow.lang.map([1, 3, 5], function (pos) {
             return 'mtwtfss'.charAt(pos - 1);
            });
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
Type
String | Function

String to make replacements with, or a function to generate the replacements

Returns

String

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

Synopsis

glow.lang.toArray(arrayLike);

Parameters

arrayLike
Type
Object

Any array-like object

Returns

Example

var a = glow.lang.toArray(glow.dom.get("a"));
trim

Removes leading and trailing whitespace from a string

Synopsis

glow.lang.trim(str);

Parameters

str
Type
String

String to trim

Returns

String

String without leading and trailing whitespace

Example

glow.lang.trim("  Hello World  "); // "Hello World"
Documentation generated by JsDoc Toolkit 2.1.0 on Mon Jul 06 2009 11:46:20 GMT+0100 (BST)

BBC navigation

BBC © 2014 The BBC is not responsible for the content of external sites. Read more.

This page is best viewed in an up-to-date web browser with style sheets (CSS) enabled. While you will be able to view the content of this page in your current browser, you will not be able to get the full visual experience. Please consider upgrading your browser software or enabling style sheets (CSS) if you are able to do so.