Programming

Core Class

This is the core of Javascript+, here you will find examples of the basic functionality added to Javascript.

Arrays

The arrays section of the Core module may be moved just to satisfy modularity. Arrays are common and very useful aspects in javascript programming. If you come with a PHP background there are a few features that seem lacking in Javascript. Many features have been added to simplify the task of working with arrays.

index

The internal pointer is similar to PHP's method of handling arrays and array iteration. All arrays inherit a default index of Zero

var test = new Array( "test-one", "test-two" );
 
-> test.index = 0

current

The current method returns an item with the current internal pointer.

var test = new Array( "test-one", "test-two" );
document.write( test.current() );
 
-> "test-one"

first

The first method resets the internal pointer to Zero and returns the fist item of the array.

var test = new Array( "test-one", "test-two" );
document.write( test.first() );
 
-> true

last

The last method resets the internal pointer to the last index and returns it.

var test = new Array( "test-one", "test-two" );
document.write( test.last() );
 
-> false

next

Next increments the internal pointer by one and returns the item with that index.

var test = new Array( 1, 2, 3, 4, 5 );
 
while( elem = test.next() )
{
	document.write( elem );
}
 
-> 12345

previous

Previous decreases the internal pointer by one and returns the item with that index.

var test = new Array( 1, 2, 3, 4, 5 ).end();
 
while( elem = test.previous() )
{
	document.write( elem );
}
 
-> 54321

reset

Reset resets the internal pointer to Zero and returns the first item. If you are iterating through arrays a lot and are not sure if the arrays pointer is in the right place always reset the array before iterating.

var test = new Array( 1, 2, 3, 4, 5 );
 
while( elem = test.previous() )
{
}
 
document.write( test.reset() );
 
-> 1

recursiveReset

Reset resets the internal pointer to Zero of all arrays within array in question.

var test = new Array( 1, 2, 3, 4, 5 );
 
while( elem = test.previous() )
{
}
 
document.write( test.reset() );
 
-> 1

end

Sets the internal pointer to the last index. This is useful if you wish to iterate backwards.

var test = new Array( 1, 2, 3, 4, 5 ).end();
 
while( elem = test.previous() )
{
	document.write( elem );
}
 
-> 54321

forEach

For each is a implementation finding its ways into most common web browsers except for Internet Explorer. This addresses that issue. Here is the MDC site for forEach

var writer = {
    sb:       [],
    write:    function (s) {
        this.sb.push(s);
    },
    writeln:  function (s) {
        this.write(s + "\n");
    },
    toString: function () {
        return this.sb.join("");
    }
};
 
[2, 5, 9].forEach(writer.writeln, writer);
print(writer.toString()); // assumes print is already defined
// Prints:
// 2
// 5
// 9

print

Prints an array. This method is likely to change.

var test = new Array( 1, 2, 3, 4, 5 );
test.print();
 
-> 12345

Elements

Talk about element modification

hasClass

Returs boolean if text string is found within the elements className attribute.

document.write( document.body.hasClass( "test" ) )
 
-> test.index = flase

addClass

Appends class name to className variable of an element and returns element to allow for chaining.

document.body.addClass( "test" ).addClass( "red" );
document.body.addClass( "test red" );

addClasses

Plural version of above method. Appends class name to className variable of an element and returns element to allow for chaining.

document.body.addClass( "test", "red" );

removeClass

Removes class name to className variable of an element and returns element to allow for chaining.

document.body.addClass( "test" ).removeClass( "red" );

removeClasses

Plural version of above method. Removes class name to className variable of an element and returns element to allow for chaining.

document.body.removeClasses( "red", "blue" );

Strings

Talk about string modification

upperCaseFirst

Uppercases first letter of a given string.

"my name is Yvon".upperCaseFirst();
 
-> "My name is Yvon"

truncate

Remove white space before and after.

"   my name is Yvon  ".truncate();
 
-> "my name is Yvon"

trim

Remove white space before and after.

"   my name is Yvon  ".trim();
 
-> "my name is Yvon"

rtrim

Remove white space before.

"   my name is Yvon  ".rtrim();
 
-> "my name is Yvon  "

ltrim

Remove white space after.

"   my name is Yvon  ".ltrim();
 
-> "   my name is Yvon"

entityify

Remove white space after.

"   my name is Yvon  ".ltrim();
 
-> "   my name is Yvon"

quote

Replaces quotes to have a quote safe string. Get more info from original website.

supplant

Replaces in-string variable. Get more info from original website.