I am new to javascript object oriented programming. I wish to write a JS class which has some variable and methods.
I am trying this way:
function B(){
function makeB()
{
alert('make B');
}
}
var b = new B();
b.makeB();
It shows: Uncaught TypeError: Object #<B> has no method 'makeB'
Can't I declare a function like this? But by simply adding this
with the same, I am able to access it. Is it a local function within the variable.?
Apart from this, I tried this:
function B(){
function call()
{
alert('B call');
}
this.makeB = function(){
call();
alert('makeB');
}
}
var b=new B();
b.makeB();
Using this, makeB
can internally call call
but if I try to access this using prototype it doesn't
function B(){
function call()
{
alert('B call');
}
}
B.prototype.makeB = function()
{
call();
alert('callB');
}
Here again I am not able to call call
from makeB
as if call
is block specific function.
And what to do if I want any function to be private to a specific class and not to inherited by the child class. A method which can only be used by its object and not by object of inherited class.
function C(){}
C.prototype = new B();
C.prototype.constructor = C;
Say, I want object of C
to call makeB
but not call
. In this case what should I do.?