Giant Examples

Creating a new classA in Giant within global namespace like projectName. will be like

 
// creating new global namespace
    $projectName = {}; 
// creating new class named ClassA
    $oop.postpone($projectName,'ClassA', function(ns, classname){
    	var base = $oop.Base,
          	self = base.extend();

      	$projectName.ClassA = self.addMethods({
       	    init: function(){
            	console.log("I am classs A");
       	    }
      	});
    });

// create a new object of classA
var objA = $projectName.ClassA.create(); // giant will internally call the init method on creating a new object of a class.

Creating a new classB and extending it by classA

    $oop.postpone($projectName, 'ClassB', function(ns, classname) {
        var base = $projectName.ClassA,
            self = base.extend();
        $projectName.ClassB = self.addMethods({
            init: function() {
                base.init.call(this);
                console.log("I am classs B");
            }
        })
    });
    $projectName.ClassB.create();

Now suppose we are creating a project and want some utility classes for project we can use giant OOP class to create some classes like.

// First we create a new namespace for utils clsses 
$utils = {};

//Now we will create a class StringUtils class for modify strings
    $oop.postpone($utils, "StringUtils", function(ns, classname){
        var base = $oop.Base,
            self = base.extend();

        $utils.StringUtils = self.addMethods({
            splitString: function(str){
                $assertion.isString(str, "Invalid String value provided");
                return str.split(" ");
            },
            trimString: function(str){
                $assertion.isString(str, "Invalid String value provided");
                return str.trim();
            }
        })
    });

    $utils.StringUtils.splitString("a b c"); // [a,b,c]
    $utils.StringUtils.splitString(123); // throws exception for wrong string value provided
    $utils.StringUtils.trimString(' abc '); // "abc"

Leave a Reply

Your email address will not be published. Required fields are marked *