Showing posts with label Function Pointer. Show all posts
Showing posts with label Function Pointer. Show all posts

Object Oriented Programming in JavaScript

There are several ways to do this, such as using prototype and JavaScript object. However, simplest may be the best. Following method matches the orthodox structure of OO, which is put everything inside of a class file:

; var ObjectClass = function(inputPara)
{
; var privateAttribute_1 = inputPara
; this.publicAttribute_Temp = 200
; function privateMethod_SetInternally()
{ ; privateAttribute_1 = 100
}
; this.publicMethod_1 = function()
{ ; privateMethod_SetInternally()
; return 50*privateAttribute_1
}
; this.publicMethod_Get = function()
{ ; return privateAttribute_1
}
; this.publicMethod_SetExternally = function(valueSetLater)
{ ; privateAttribute_1 = this.publicMethod_1*valueSetLater*123456789
}
}

Please note, for private method, do not use Varibalized Function, which is also called Function Literal, Function Reference or Function Pointer, like this:

; var privateMethod_SetInternally = function () {…}

Indeed, private method privateMethod_SetInternally is useless here, because you can always set internally directly:

; privateAttribute_1 = newValue

The purpose of privateMethod_SetInternally here is to show how to call this method, as stated in publistMethod_1. There is no way for outsider to use this private method, but through publistMethod_1.

All attributes/method started with this. is the public accessible by outsider caller, or the object based on this Class.

To use this Class, an object needs to be created:

; var obj = New ObjectClass(300)
; var att1_inThere = obj.publicMethod_Get()
; obj.publicMethod_SetExternally(400)

Please note, internally, calling all public accessible attributes/methods would not need with (), while private methods need to include (). However, for outsider object, public methods need to include () as well, like above.

There is a problem. Since internally calling public methods could not be with (), there is no way to pass the parameter while calling. In here, publicMethod_SetExternally can be called and assigned with parameters externally with no problem, but can only be called without assigning parameter internally. In this case, parameter is essential, so it would generate “not defined” error.

To overcome this, publicMethod_SetExternally needs to be rewritten as follows:

; this.publicMethod_SetExternally = function()
{ ; privateAttribute_1 = this.publicMethod_1*this.publicAttribute_Temp*123456789
}

Internally, it can now be called directly:

; this.publicMethod_SetExternally

Externally, it would be called like this:

; obj.publicAttribute_Temp = 400
; obj.publicMethod_SetExternally()

Javascript Error Message: is not a function

This error message appears when you use Varibalized Function (or called Function Literal, Function Reference, Function Pointer) method to declare function while you did not put into right order:

(Caller here)
; var func = function()
{
...
}

Solution 1:

; var func
(Caller here)
; func = function()
{
...
}

Solution 2:

(Caller here)
; function func()
{
...
}


http://www.dustindiaz.com/javascript-function-declaration-ambiguity/
Function Declaration in JavaScript

Function Declaration in JavaScript

It is easy to declare a function in JavaScript. And everyone knows it:

Standard or Procedural Function

; function func()
{
...
}

The problem of it is this function cannot be passed as parameter in another function. To be able to do that, you will need to varibalize it:

Varibalized Function, or Function Literal, Function Reference, Function Pointer

; var func = function()
{
...
}

Everyone does that too. Wait a moment. This form for declaration indeed is just set to fail if you use it carelessly. You may get following error massage when sequence to execute programming block becoming sensitive, such as work with setTimeout() and setInvertal() or inside of a function:

[This function] is not defined.

If you include this form of function with setInterval or setTimeout, you may get error message:

useless setInterval call (missing quotes around argument?)

This is because you will need to put it in right sequence to make sure the memory has load the function first.

So, it is suggested in general you should use orthodox function declaration method. If you use variablized function declaration method, please make sure it has been put into right place in the sequence of programming blocks.

Have a look at following case:

; var func = func()

; function func()
{
...
}

No, this is not a function declaration method for first statement. It just get the return value from the function func().

Apart from above mentioned popular method, here is

Function Object

; var func = new Function()
{
...
}

It is not recommended to use because it would take longer time to process, unless for special purpose, such as reconstruct to function from variablized function and variablized parameters. For instance:

; var reconstructedVar = new Function([para1, para2,...paran], functionBody)
{
...
}

Please take note, all parameters and function body must be in string or reference. The other way to reconstruct the function is to do it by yourself. For details, please see Passing Function in JavaScript.


http://www.permadi.com/tutorial/jsFunc/index.html
http://www.hunlock.com/blogs/Functional_Javascript
http://www.dustindiaz.com/javascript-function-declaration-ambiguity/
http://osdir.com/ml/jQuery/2009-11/msg01942.html
Javascript Error Message: is not a function

Labels