Menu
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]
<?php
class object
{
public static function F1()
{
...
}
}
object::F1();
?>
[/CODE]
?<i>
</i>myConstructor=function(){
this.static_function1=function(){
...
}
this.static_function2=function(){
...
}
}
var o=new myConstructor();
o.static_function1();
o.static_function2();
<i>
</i>myConstructor=function(){}
myConstructor.prototype.static_function1=function(){
...
}
myConstructor.prototype.static_function2=function(){
...
}
var o=new myConstructor();
o.static_function1();
o.static_function2();
[CODE]var o=new myConstructor(); [/CODE]
else it wouldnt be realy static would it ?[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]
[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 ?<i>
</i><script type="text/javascript">
myConstructor={
'str':'hello',
'static_function1':function(){alert(this.str)},
'static_function2':function(){alert(this.str)},
};
myConstructor.static_function1();
myConstructor.static_function2();
</script>
<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(' ')
[CODE]myConstructor={
'str':'hello',
'static_function1':function(){alert(this.str)},
'static_function2':function(){alert(this.str)},
};[/CODE]
ok now this is realy cool i dont understand it yet but never the less its cool ?
[/QUOTE]
JSON (JavaScript Object Notation) is a lightweight data-interchange format.[/QUOTE]?
<i>
</i>var myVariable='Hello World';
window['myFunction']=function(){alert(window['myVariable'])}
onload=function(){
window['myFunction']()
}
[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]
<i>
</i><script type="text/javascript">
function varTest(){
private1="varTest->private1"
}
varTest()
alert(private1)//varTest->private1
</script>
<i>
</i><script type="text/javascript">
function varTest(){
[color=blue]var[/color] private1="varTest->private1"
}
varTest()
alert(private1)//error, undefined
</script>
[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]
<i>
</i><script type="text/javascript">
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)
</script>
[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]
<i>
</i><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")
alert(me.getPrivateClass().private_name)
alert(me.height)
</script>
[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]
[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]
<i>
</i><body>
<div id="c">
<script type="text/javascript">
[color=blue]String.prototype.toTextNode[/color]=function(){
return document.createTextNode(this);
}
document.getElementById("c").appendChild("blah".toTextNode())
</script>
</div>
</body>
<i>
</i>myConstructor=new String("1,2,3");
myConstructor.split(",")
<i>
</i><script type="text/javascript">
[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<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())
</script>
[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]
0.1.9 — BETA 6.16