About Pi Downloads Documentation Plugins Developer Forum Issues

pi.util.Extend

Summary

Provides creating javascript classes by using pi.base.With this function, you can extend a class with other classes. If not another class will be extended, pi.base should be send as the first parameter.

Syntax

var className = pi.util.Extend( superClass, { "$Init":Constructor, property name:value, private member:value }, boolean skipClonning (optional) )

General Usage

>>> var myClass = pi.util.Extend(
	pi.base,
	{
		"$Init":function(_argument){
			this.properties.setProperty(_argument);
		};
		"method":function(){
			return "Hello World";
		},
		"properties":{ // pi.base clones the hash members automatically
			"_property":0 // private member and getter/setter methods are created automatically.
		}
	}
);
>>> var a = new myClass;
>>> myClass.$Init
function

Examples

Example #1: Create a class "person" with a class "user" which is extending the class. To test the extended class use trace.

Source files of the above example:

../../examples/extend.html

<div id="container"></div>

../../examples/extend.js

var person = pi.util.Extend(
	pi.base,
	{
		"$Init":function(_name,_age,_country){
			this.environment.setName(_name).environment.setAge(_age).environment.setCountry(_country);
		},
		"environment":{
			"_name":"", "_age":0, "_country":""
		}
	}
);

var eto = new person("Eto Demerzel","30","Aurora");

var user = pi.util.Extend(
	person
	,
	{
	"$Init":function(_email,_password,_name,_age,_country){
		person.$Init.call(this,_name,_age,_country);
		this.environment.setEMail(_email).environment.setPassword(_password);
	},
	"environment":{
		"_eMail":"", "_password":"",
		"_name":"", "_age":0, "_country":""
	}
});

var seldon = new user("hseldon@streeling.edu","dors123raych","Hari Seldon","70","Trantor");

pi.util.Init.push(function(){
	trace(
		pi.get("container"), // asagidaki bilgiler bu element'in icine yazdirilacak.
		"<strong>Person: eto</strong>",
		"name: "+eto.environment.getName(),
		"age: "+eto.environment.getAge(),
		"country: "+eto.environment.getCountry(),
		"<strong>User: seldon</strong>",
		"email: "+seldon.environment.getEMail(),
		"password: "+seldon.environment.getPassword(),
		"name: "+seldon.environment.getName(),
		"age: "+seldon.environment.getAge(),
		"country: "+seldon.environment.getCountry()
	);
});

See Also