I wrote the following class in javascript
function main()
{
this.area ;
this.$= function(target)
{
if(target == undefined)
{
this.area = document;
}
else
{
this.area = document.getElementById(target);
if( this.area == null )
{
this.area = document.getElementsByTagName(target);
if( this.area[0] == null )
alert("Error value "+target+" passed to $() is no an id or name");//code to create variable
}
}
}
this.display = function(value)
{
if(this.area == undefined)
alert("Error in display() method...target not selectedddd");
else area.innerHTML = value;
}
}
and i always get the error message I given as alert. Why area is always null?
If I refer to area without using the this keyword will it work. Is there any way to nest classes in javascript like
function()
{
this.a;
function()
{
}
}
The reason why it does not work is that
thisrefers to the inner function scope, not the outer function scope you call a “class”.Try something like this instead: