Hi all I come from C,C++,Visual Basic,Java,Perl,PHP and then now to Javascript. My knowledge of Javascript is still at those late 90’s era where it is used for simple scripting purposes.
Recently due to the need to learn AJAX coding, I discover Javascript has changed ALOT!!! In fact I find the different kind of syntax to create objects in Javascript mind-boggling!!! I have below questions to ask.
1.
Is there a Interface concept in Javascript ?
Is there a abstract class concept in Javascript ?
Is there final static variables concept in Javascript ?
Are there private, protected, public fields/methods concept in Javascript ?
Is there sub-classing and inheritance concept in Javascript ?
Does Javascript support multiple inheritance ?
Does Javascript support template like C++ ?
For each of the questions above I would appreciate some code snippets as I am trying to learn the OOP Javascript syntax which I believe is very counter-intuitive to a developer whose background is from those classical OOP languages like C++ and Java.
Thanks.
[CODE]function check(){
const y={a:1};
y.a=5;
return y;
}
alert(check().a)//===5[/CODE]
[CODE](function(){
var x=123;
showX=function(){alert(x);}
}());
x=99;
showX();//123[/CODE]
[CODE]function count(){
return ++count.total;
}; count.total=0;
alert([ count(), count(), count(), count(), count() ]);
//shows: 1,2,3,4,5[/CODE]
[CODE]var o={
counter:
function count(){
count.total=count.total||0
return ++count.total;
}
};
alert([ o.counter(), o.counter(), o.counter(),
o.counter(), o.counter(), "Total:", o.counter.total ]);
//shows: 1,2,3,4,5,Total:,5[/CODE]
[CODE]
function Demo(){
function getter(){return private;} //private
var private=555;
this.public=123;
function hi(){alert("hi");} // private
this.hello=function(){alert("hello");} // public
this.getPrivate=function(){alert(private);} // privileged method
this.invited={valueOf: getter, toString: getter}; // privileged property
Demo.lastBuilt=this; // "static"
}
Demo.prototype.proto=function(){alert(this.public);} //prototype method
var obj=new Demo();
[/CODE]
[CODE]var ray=[1,2,3];
var dupe=ray;
dupe[1]="Fred";
ray[3]=false;
alert(ray); //shows: 1,Fred,3,false[/CODE]
[CODE]var obj={a:1, b:2, c:3};
with(obj){
function adder(){
alert(a+b+c);
}
}//end with
adder()//===6
obj.b=10;
adder()//===14[/CODE]
[CODE]
function Life(){
this.type="life"
this.alive=true;
}
function Animal(){
this.type="animal";
this.age=0;
this.die=function(){this.alive=false;}
}
function Person(){
this.type="human";
this.birthday=(new Date).toLocaleDateString();
}
//join the constructors by linking thier prototypes:
Animal.prototype=new Life;
Person.prototype=new Animal;
var me= new Person, props=[];
for(property in me){
props.push( property +":t"+ me[ property ] );
}
alert(props.join("n"));
/* shows:
type: human
birthday: Wednesday, March 24, 2010
age: 0
die: function () {
this.alive = false;
}
alive: true */
[/CODE]
[CODE]
//making "new " prefix optional:
function Constructor(args){
if(this.Array==Array){return new Constructor(args);}
this.arg=args;
}
var x= new Constructor("fred");//==={ arg="fred"}
var y= Constructor("fred");//==={ arg="fred"}
//overloading/flexible args
function el(id){
if(id.nodeName){return id;} //quack
if(id.splice){return id.map(el);}//quack
return el._ts[id]||(el._ts[id]=document.getElementById(id));
}; el._ts={};
[/CODE]
I'll bet that in a large room of programmers, one of them will disagree with everything i said above.[/QUOTE]
For this reason (but not only) JavaScript is a slow language (about 100 times slower than C++, for instance)[/QUOTE]
perhaps we all need to reconsider the 100X rule.
[/QUOTE]
Agree. But when I am thinking of C#, (not C++), I wonder...?[/QUOTE]
... Does C# produce faster binaries than C++?[/QUOTE]
By the way, [B]sohguanh[/B], there is another aspect: JavaScript is an [I]interpreted[/I] language, not a [I]compiled[/I] one.[/QUOTE]
Regarding AJAX: AJAX is nothing but a technique, not a language. AJAX is based on 3 foundation stones: 1. the request object (XMLHttpRequest or, for IE <=7: ActiveX ) , 2. JavaScript, 3 (optional) XML, If the request is not for an XML file, you need an extra server-side application (written in PHP, or ASP, or Pearl, or Java, or whichever...) to handle the request to and from a DB.[/QUOTE]
Not a bright idea to use .NET (probably you mean ASP .NET) for websites. php/MySQL is the winner pair, for the moment. [/QUOTE]
If you want to use C++ in a linux/unix/bsd environment, you'll want to figure out how to write apache modules. You could write binaries that work as CGI's, but CGI's are horribly inefficient. (The fire up a new process for [B]every[/B] HTTP request that hits them.)
PHP is probably the most [I]efficient[/I] and affordable option. (Though you can get VS [I]Express[/I] editions for free, you still have to pay for the Windows OS to serve the site from.)[/QUOTE]
Hi all after a week or so of intensive reading, I have hopefully (?) capture and learn the Javascript OOP syntax. I know ppl will say learning syntax is trivial but coming from the C,C++,Java background, the Javascript OOP syntax require some mindset changes.
[/QUOTE]
0.1.9 — BETA 6.2