Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Working with objects in JS

The starting point to get confidence over JavaScript OO (object-oriented approach) full support take a look at this post (http://nefariousdesigns.co.uk/archive/2006/05/object-oriented-javascript/). Emulation? Right - these are not C++ or Java techniques with deidcated keywords (protected, private, abstract, extends, template...) and operators (::, ->, : ...). But - it (JS) works and fits pretty well to develop GUI, test automation harness, tools and utilities. Keep it simple, right?

Hovewer the primary intention of the post is to give Ready-to-Go utilities to work with JS's objects as data structures for TestComplete (JS) users. The excerpt is here:

 //USEUNIT TypeTraits  
function Clone ( obj )
{
if (!TypeTraits.IsReference ( obj ))
{
return obj;
}
var newObj = new Object ( );
for ( var key in obj )
{
newObj [ key ] = Clone ( obj [ key ] );
}
return newObj;
}
function toString( obj )
{
var _collectionToString = function( obj, indent ){
var newIndent = indent || "";
newIndent += " ";
var pairs = [];
for (var key in obj)
{
if ( obj[key] === null )
{
var value = "null";
}
else if ( typeof obj[key] == "object" )
{
var value = _collectionToString( obj[key], newIndent )
}
else
{
var value = obj[key];
}
pairs.push( "\n"+newIndent+key+" : "+value );
}
return ( "{"+pairs+"\n"+indent+"}" ).replace( /\{[\n\s]+\}/mg,"{}"); //remove all spaces between empty {}
}
return _collectionToString( obj, "" )
}
function merge( /*Object*/ base, /*Object*/ another )
{
/*
PURPOSE:
Merge base object with another object
RETURNS
Merged object.
Fields of "base" replaced with those of "another"
*/
//Clone base into resulting object to avoid damage to base object
var result = new Object();
if ( base )
{
for ( property in base )
{
if ( base.hasOwnProperty(property) )
result[property] = base[property];
}
}
// merge
if ( another )
{
for ( property in another )
{
if ( another.hasOwnProperty(property) && ( another[property] !== null) )
result[property] = another[property];
}
}
return result;
}


This post first appeared on AT4QA, please read the originial post: here

Share the post

Working with objects in JS

×

Subscribe to At4qa

Get updates delivered right to your inbox!

Thank you for your subscription

×