/    Sign up×
Community /Pin to ProfileBookmark

name_obj.static_function()

How do you make a static function in js? i tought there was a trick to do this somehow with ‘self’

Maybe this would work ?

[CODE]name_obj=function()
{
self.static_function()
{

}
}

name_obj.static_function()
[/CODE]

to post a comment
JavaScript

23 Comments(s)

Copy linkTweet thisAlerts:
@KorSep 01.2006 — What do u mean by static function?
Copy linkTweet thisAlerts:
@gert_cuykensauthorSep 01.2006 — static like in [CODE]
<?php
class object
{
public static function F1()
{
...
}
}

object::F1();
?>
[/CODE]
?
Copy linkTweet thisAlerts:
@UltimaterSep 01.2006 — I think you are getting into constructors and prototypes.

<i>
</i>myConstructor=function(){
this.static_function1=function(){
...
}
this.static_function2=function(){
...
}
}
var o=new myConstructor();
o.static_function1();
o.static_function2();

or
<i>
</i>myConstructor=function(){}
myConstructor.prototype.static_function1=function(){
...
}
myConstructor.prototype.static_function2=function(){
...
}
var o=new myConstructor();
o.static_function1();
o.static_function2();
Copy linkTweet thisAlerts:
@gert_cuykensauthorSep 01.2006 — yes but i want to get rit of [CODE]var o=new myConstructor(); [/CODE] else it wouldnt be realy static would it ?

And i think i just found out how too ? [CODE]myConstructor=function()
{
self.str="hello"
}
myConstructor.static_function1=function()
{
alert(myConstructor.str)
}
myConstructor.static_function2=function()
{
alert(myConstructor.str)
}

myConstructor.static_function1();
myConstructor.static_function2();[/CODE]


except self doesnt exist :rolleyes:
Copy linkTweet thisAlerts:
@gert_cuykensauthorSep 01.2006 — bingo! [CODE]myConstructor=function(){}
myConstructor.str='hello';
myConstructor.static_function1=function(){alert(myConstructor.str)}
myConstructor.static_function2=function(){alert(myConstructor.str)}
myConstructor.static_function1();
myConstructor.static_function2();[/CODE]
You got to love js for its stuburnis ?
Copy linkTweet thisAlerts:
@UltimaterSep 01.2006 — You should really be using an Object.
<i>
</i>&lt;script type="text/javascript"&gt;
myConstructor={
'str':'hello',
'static_function1':function(){alert(this.str)},
'static_function2':function(){alert(this.str)},
};
myConstructor.static_function1();
myConstructor.static_function2();
&lt;/script&gt;

I hope the following will enhance the understanding of the similarities between PHP and JS -- not that I feel you lack skills in either PHP or JS however upon the hope that it will assist someone out there -- even if the only person out there it helps is me just by writing it because I'm still in the learning process regarding PHP yet my PHP skills are relatively high nonetheless.
<i>
</i>[b]PHP: Java[/b][b]Script:[/b] [b]NOTES:[/b]

$self this
extends prototype
class function
global window. Variables in functions in JS are global by default
local var Variables in functions in PHP are local by default
elseif else if
create_function('...') Function('...')
func_get_args() arguments
func_num_args() arguments.length
implode(' ',$myArray) myArray.split(' ')
explode(' ', $myString) myString.join(' ')
Copy linkTweet thisAlerts:
@gert_cuykensauthorSep 02.2006 — ok now this is realy cool i dont understand it yet but never the less its cool ?
[CODE]myConstructor={
'str':'hello',
'static_function1':function(){alert(this.str)},
'static_function2':function(){alert(this.str)},
};[/CODE]


PS i like the list allot , maybe you could make the list sticky in this forum and atach some more items to it.
Copy linkTweet thisAlerts:
@KorSep 02.2006 — ok now this is realy cool i dont understand it yet but never the less its cool ?
[/QUOTE]

[URL=http://www.json.org]JSON[/URL]
Copy linkTweet thisAlerts:
@gert_cuykensauthorSep 02.2006 — Do you know that feeling when you are reading something and you actualy understand less about it after you read it ? I have no idea what JSON is except its from 1999 lol

JSON (JavaScript Object Notation) is a lightweight data-interchange format.[/QUOTE] ?
Copy linkTweet thisAlerts:
@gert_cuykensauthorSep 02.2006 — after reading it like 10 times i think my brains finaly know what the drawing means that looks like a guy with funny ears.

http://www.json.org/object.gif

so if i understand it correctly JSON is just some standard notion like for example html
Copy linkTweet thisAlerts:
@KorSep 03.2006 — You have used some JSON for years without know it. What is so difficult there?

By short JSON replaced

[COLOR=DarkGreen]var myArray = new Array()[/COLOR]

with

[COLOR=Blue]var myArray = [ ][/COLOR]

and

[COLOR=DarkGreen]var myObj = new Object()[/COLOR]

with

[COLOR=Blue]var myObj ={ }[/COLOR]

It is not a new language. It is just a structured alternative notation based on a common core of all the languages. In some ways it is an alternative to XML. But not this feature is related to your problem, but the possibility to create/handle an object and it's properties.

JSON uses the fact that Javascript is an OO Language (Object Oriented Language) and it can handle [I]everything[/I] (document's elements, other objects, variables, functions, methods) as [B]objects[/B] [I]and[/I] as [B]properties[/B] of their [I]parentObject[/I]

[COLOR=DarkGreen]var myVariable[/COLOR]

can be written as

[COLOR=Blue][I]parentObject[/I]['myVariable'][/COLOR]

[COLOR=DarkGreen]myFunction()[/COLOR]

can be written as well as:

[COLOR=Blue][I]parentObject[/I]['myFunction'][/COLOR]

and can be called as:

[COLOR=Blue][I]parentObject[/I]['myFunction']()[/COLOR]

ex:
<i>
</i>var myVariable='Hello World';
window['myFunction']=function(){alert(window['myVariable'])}
onload=function(){
window['myFunction']()
}

and so on...

JSON is nothing but a structured "brackets notation"
Copy linkTweet thisAlerts:
@CrazyMerlinSep 03.2006 — very interesting...thx Kor!
Copy linkTweet thisAlerts:
@gert_cuykensauthorSep 03.2006 — So if this works ? http://www.dustindiaz.com/json-for-the-masses[CODE]function Person(name,height){
return {
name:name,
height:height,
getName:function(){return this.name},
getHeight:function(){return this.height}
}
}[/CODE]
Would this work too ?[CODE]function Person(name,height){
private=
{
private_name:name,
private_getName:function(){return this.name},
}
public=
{
height:height,
getHeight:function(){return this.height}
}
return public
}[/CODE]
Copy linkTweet thisAlerts:
@UltimaterSep 03.2006 — 
  • 1. "private" is a reserved keyword identifier in JavaScript as well as "public".

  • 2. Remember, variables in JavaScript are global by default.
    <i>
    </i>&lt;script type="text/javascript"&gt;
    function varTest(){
    private1="varTest-&gt;private1"
    }
    varTest()
    alert(private1)//varTest-&gt;private1
    &lt;/script&gt;

    <i>
    </i>&lt;script type="text/javascript"&gt;
    function varTest(){
    [color=blue]var[/color] private1="varTest-&gt;private1"
    }
    varTest()
    alert(private1)//error, undefined
    &lt;/script&gt;
  • Copy linkTweet thisAlerts:
    @gert_cuykensauthorSep 03.2006 — ok what about this ?

    [CODE]function person(name,height)
    {
    var privateClass=
    {
    private_name:name,
    private_getName:function(){return this.name}
    }
    publicClass=
    {
    height:height,
    getHeight:function(){return this.height}
    }
    return publicClass
    }[/CODE]
    Copy linkTweet thisAlerts:
    @UltimaterSep 03.2006 — privateClass would be only accessibile within the function person. Depends how you wish to use the function.
    <i>
    </i>&lt;script type="text/javascript"&gt;
    function person(name,height)
    {
    var privateClass=
    {
    private_name:name,
    private_getName:function(){return this.name}
    }
    publicClass=
    {
    height:height,
    getHeight:function(){return this.height},
    getPrivateClass:function(){return privateClass}
    }
    return publicClass
    }
    alert(person("John","6"4").getPrivateClass().private_name)
    alert(publicClass.height)
    &lt;/script&gt;
    Copy linkTweet thisAlerts:
    @gert_cuykensauthorSep 03.2006 — actualy if i could use it like this would be nice

    me=person("John","6"4")

    me.getPrivateClass().private_name

    me.height

    So i think there need still a var before public ?

    [CODE]<script type="text/javascript">
    function person(name,height)
    {
    var privateClass=
    {
    private_name:name,
    private_getName:function(){return this.name}
    }
    var publicClass=
    {
    height:height,
    getHeight:function(){return this.height},
    getPrivateClass:function(){return privateClass}
    }
    return publicClass
    }
    me=person("John","6"4")
    alert(me.getPrivateClass().private_name)
    alert(me.height)
    </script>[/CODE]
    Copy linkTweet thisAlerts:
    @UltimaterSep 03.2006 — Correct that would work and would be a proper usage of "var".

    It would be even better to use "this" instead of "return".
    <i>
    </i>&lt;script type="text/javascript"&gt;
    function person(name,height)
    {

    var privateClass={private_name:name, private_getName:(function(){return this.name}) }

    this.height=height;
    this.getHeight=function(){return this.height};
    this.getPrivateClass=function(){return privateClass};

    }

    me=new person("John","6"4")
    alert(me.getPrivateClass().private_name)
    alert(me.height)
    &lt;/script&gt;

    Also there is a convention to capitalize the first letter of each constructor's names. i.e. Person
    Copy linkTweet thisAlerts:
    @gert_cuykensauthorSep 03.2006 — Yep thats defenatly a great way to make persons ?

    PS your new Person() way is also compatilble with inheriting (prototyping) so i think this would be it

    [CODE]<script type="text/javascript">
    function Person(name,height)
    {
    var privateClass=
    {
    private_name:name,

    private_getName:(function(){return this.name})
    }
    this.height=height;
    this.getHeight=function(){return this.height};
    this.getPrivateClass=function(){return privateClass};
    }

    me=new Person("John","6"4")
    me.prototype.age='25'
    alert(me.getPrivateClass().private_name)
    alert(me.height)
    alert(me.age)
    </script>[/CODE]
    Copy linkTweet thisAlerts:
    @UltimaterSep 03.2006 — me.prototype.age='25' will generate an error.

    use me.age='25' instead.

    Prototypes are only to be used to extend constructors not objects created by constructors.

    Could also do something like:

    me.constructor.prototype.age=14

    which is the same as:

    Person.prototype.age=14

    or throwing

    this.age=14

    directly into the Person function

    However if you created a new person "me2" me2.age would be automatically set to 14.
    Copy linkTweet thisAlerts:
    @gert_cuykensauthorSep 03.2006 — So if we would like to extend person with a function called hmm lets say 'grow()' That increments the persons height we would do this like this ?

    [CODE]<script type="text/javascript">
    Person=function(name,height)
    {
    var privateClass=
    {
    private_name:name,

    private_getName:(function(){return this.name})
    }
    this.height=height;
    this.getHeight=function(){return this.height};
    this.getPrivateClass=function(){return privateClass};
    }

    Person.prototype.grow=function(){this.height++;}

    me=new Person("John",7)
    me.age='25'
    me.grow()
    alert(me.getPrivateClass().private_name)
    alert(me.height)
    alert(me.age)
    </script>[/CODE]
    Copy linkTweet thisAlerts:
    @UltimaterSep 03.2006 — Bingo.


    Prototypes are used to extend constructors. Your constructors are not limited to just user-defined constructors.

    Function, Object, Array, String, Date, Number, Boolean, Option, Image are all constructors.

    There are many other built-in constructors that you can extend with prototypes.

    e.g. The HTMLElement is not cross-browser but it CAN be made cross-browser http://www.browserland.org/scripts/htmlelement/

    A simple extension of the String constructor can be made:
    <i>
    </i>&lt;body&gt;
    &lt;div id="c"&gt;
    &lt;script type="text/javascript"&gt;
    [color=blue]String.prototype.toTextNode[/color]=function(){
    return document.createTextNode(this);
    }
    document.getElementById("c").appendChild("blah".toTextNode())
    &lt;/script&gt;
    &lt;/div&gt;
    &lt;/body&gt;

    Ever execute some code like "1,2,3".split(",") before?

    Really what you are doing is:
    <i>
    </i>myConstructor=new String("1,2,3");
    myConstructor.split(",")

    Hence you can even alter the default behaviour of String.prototype.split



    A more complex example would be:
    <i>
    </i>&lt;script type="text/javascript"&gt;
    [color=blue]Array.prototype.getAddedKeys[/color]=function(){var a=this,kArray=[],vArray=[],i,l,nArray=[];for(i in a){if(i in Array.prototype)continue;kArray[kArray.length]=i;vArray[vArray.length]=a[i]}l=kArray.length;for(i=0;i&lt;l;i++){nArray[nArray.length]=kArray[i];nArray[nArray.length]=vArray[i];}return nArray}

    var meArray=["zero","one","two","three"];
    meArray[5]="five"
    meArray["number9"]=9

    alert(meArray.getAddedKeys())
    &lt;/script&gt;


    Might want to check out the [post=347143]Prototype Function Collection[/post] thread
    Copy linkTweet thisAlerts:
    @gert_cuykensauthorSep 18.2006 — [CODE]<script type="text/javascript">
    Person=function(name,height)
    {
    var private=
    {
    name:name,

    b:(function(){alert(name+' born')}),
    age:1,
    height:height
    }
    this.public=
    {
    name:(function(){alert(private.name)}),
    age:(function(){alert(private.age)}),
    height:(function(){alert(private.height)})
    }
    private.b()
    this.public.age()
    }

    Person.prototype.height=function(){this.public.height()}

    me=new Person("John",7)
    me.height()
    me.public.height()
    </script>[/CODE]


    My new Jstyle ?
    ×

    Success!

    Help @gert_cuykens spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...