Is it possible to have a Javascript function to have two or more functions inside? -
i looking @ this:
function lab = { minseat: function(){ var seat = 10; return seat; }, maxseat: function(){ var seat = 50; return seat; } }
the problem because following code run @ runtime , positioning of code become important need place them above line of code (e.g. lab.minseat()
) call upon it.
var lab = { minseat: function(){ var seat = 10; return seat; } maxseat: function(){ var seat = 50; return seat; } }
yeah, know question stupid , attract down vote want know if possible?
you trying use function literal.
in javascript there thing called literal works this, , yes can have more one. syntax this:
var foo = { bar: functon(){}, foobar: function(){} }
you can call these functions this
foo.bar(); foo.foobar();
if wanted function have functions can call outside of function can either use prototype keyword:
function foo(){}; foo.prototype.bar = function(){};
you can call these functions this
var f = new foo(); f.bar();
or can use keyword
function foo(){ this.bar = function(){}; }
you can call like
var f = new foo(); f.bar();
or if going use functions in function can use var keyword
function foo(){ var bar = function(){}; }
you can call inside function this
bar();
if have ever done oo programming classes might bit easier explain.
think of function class. has public , private members , functions, inside function can specify these, when using var
keyword think of these private members , functions, , when using this
keyword think of these public ones, prototype
members work pretty this
still havent looked difference.
when using var
, this
, prototype
function these non static
in terms of object oriented programming. needs object reference.
this because assignment done either when call function or when create new instance of
there difference between calling new foo()
compared calling foo()
when calling foo()
function returns, when calling new foo()
instance of object.
so reference public members of foo
need create new instance of it
just kinda wanted cover all. xd
Comments
Post a Comment