Classing{js} Tutorial

Object::instanceOf

Why a new instaceOf ?

With Classing{js} and its ability to make a class implements multiple interfaces in addition to inheriting a class, the ordinary instanceof operator of javascript became limited as it cannot check if a class is an instance of an interface that is implemented by it. To solve this issue, the library provides a custom method called instanceOf that gets attached to every object you create and has the ability to check if this object is an instance of a class or an interface in any level in the ancstors tree.

It's extremly simple to use , just call yourObject.instanceOf(ancestor) and it will return true if your object is an instance of that ancestor, or false otherwise.

Here's an exmaple that will make things even clearer:

var Identifiable = classing.Interface({
	getIdentified: function(){}
});

var IPayable = classing.Interface({
	getPaid : function(){}
});

var Person = classing.Class.Implements(Identifiable)({
	private : {
		id : null,
	},
	protected : {
		name : null,
		age : null,
	},
	public : {
		Construct : Function.create(xTyped , [
			types(),
			function() {
				this.id = Date.now();
			},
			types(String , Number),
			function(str , num) {
				this.name = str;
				this.age = num;
				this.id = Date.now();
			}
		]),
		getIdentified : function() { return this.id; }
	}
});

var Employee = classing.Class.Extends(Person).Implements(IPayable)({
	protected : {
		salary : 5000,
		balance : 0
	},
	public : {
		getPaid : function() {
			this.balance += this.salary;
		}
	}
});

var emp1 = new Empolyee();

emp1.instanceOf(Empolyee); //true
emp1.instanceOf(IPayable); //true
emp1.instanceOf(Person); //true
emp1.instanceOf(Identifiable); //true
emp1.instanceOf(Object); //true
emp1.instsnceOf(Array); //false