About Pi Downloads Documentation Plugins Developer Forum Issues

Function.extend

Summary

Provides creating javascript classes by using pi.base.With this method you can extend a class with other classes. If not another class will be extended, use the extend method of pi.base.

Syntax

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

General Usage

>>> var myClass = pi.base.extend({
	"$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.base.extend({
	"$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 = person.extend({
	"$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