?????? ??
JavaScript? ???? ????? ??(OOP)??? C++, Java ? ???? ???? ???? ????. ?? ?? JavaScript?? ????? ??? ????. ??? OOP ????? ?? ?????? ??? ??? ? ?? ?? ?????? ??? ???.
?? JavaScript? ?? ?? ?????. JavaScript? ?? ?? ?? ???? ? ? ????. ?? ????? ????? ??? ??? ?? ???? ????, ? ?? ???? ?? ??? ??? ??? ???? ??? ?? ?? ? ????.
??? ??????
??? ??? ???? ?????. ??? ????, ??? ??? ??? ? ?? ??? ???? ? ??? ?? ??? ?? ??? ?????. ?? ??, ??? ???? ??? ????.
??? ??: ??? ??, ?, ??? ? ?? ??? ??? ????. ??? ??? ??? ????? ???. ???? ??? ?? ? ?? ??? ??? ????? ????.
???? ???? ??? ???: ????? ??? ??? ????? ??? ? ??, ???? ?? ???? ? ? ?? ?? ??? ??? ???? ????? ???. ???, ???, ?? ? ??? ??? ?? ?? ??? ??? ?? ????? ???.
???: ????? ??? ?? ??? ? ??? ????? ??? ? ???, ?? ?? ???? ??? ???? ?? ??? ??? ?? ??? ????. ??? ?? ???? ??? ???? ? ??? ?? ???? ??? ? ????. ?? ????? ???.
??: ??? ??? ?? ?????. ???? ?? ???? ?? ????? ??(??)?? ?? ???? ??? ???(?? ?? ? ??? ??)? ?? ?? ?????. ?? ?? ? ??? ??? ??? ?? ???? ?? ????? ???? ???? ????. JavaScript ???? ??? ??? ?? ??? ?? ??? ?? ???? ????. ??? ?? ?? ???? ?? JavaScript ???????? JavaScript? ?? ??? ????? ???.
???: ???? ??? ??? ??? ??? ?? ? ?? ??? ????. ??? ????? ???? ?? ??? ?? ?? ??? ??? ?? ?????? ??? ? ???? ??? ???? ?? ???? ?????. ????? ??? ????? JavaScript? OOP?? ?? ??? ??? ???? ???? ????. ?? JavaScript?? ???? ????? ????? ???? JavaScript ?????? ?? ????.
? ????? ??? ??? ???? ???? ??? ?? ? ?? ?? ??? ???? ???? ????. ?? ?? ?????? ?? ?????. ?? ?? ???? OOP? ?? ?? ?? ??? ?? ? ????.
?? ?? ???
??? ??? ??? ????.
?? ??? ????? ?? ??? ??? ????.
objectName.propertyName
? ???? String ??? ?? ??? ???? ???? ??? ?????.
var message="Hello World!";
var x=message.length;
? ??? ??? ?? ?? ??? ?? ??? ? ?? ?????.
?? ??? ???? ???? ??? ? ????.
objectName.methodName()? ???? String ??? toUpperCase() ???? ???? ???? ???? ?????.
var message="Hello world ! ";var x=message.toUpperCase();
? ??? ??? ? x ?? ??? ????.
HELLO WORLD!
??? ???? ?? ??: <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
</head>
<body>
<script>
function person(firstname,age){
this.firstname=firstname;
this.age=age;
}
myFather=new person("John",50);
document.write(myFather.firstname + " is " + myFather.age + " years old.");
</script>
</body>
</html>
?? ???? ??? ??? ?? ? ?? ????? ?? ? ????.
var myFather=new person("John","Doe",50,"blue");var myMother =new person("Sally","Rally",48,"green");
JavaScript ??? ?? ??
??? ?? ???? ?? ??? ? ??? ??? ? ????.
personObj? ?? ????? ???? ??? ??? ?? ??? ??? ??? ? ????: ??, ?, ?? ? ? ??:
person.firstname="John";person.lastname="Doe";person.age=30;
person.eyecolor="blue";
x=person.firstname;
T? ??? ??? ? x ?? ??? ????.
John
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script> // 定義構(gòu)造函數(shù),并設(shè)定一個屬性 function Person(name) { this.name = name; } // 為 Person 增加一個方法 Person.prototype.showName = function() { alert("我叫" + this.name); }; // new 關(guān)鍵字實例化一個對象 var Tom = new Person("Tom"); // 運行該對象內(nèi)的 showName() 方法 Tom.showName(); </script> </head> <body> </body> </html>?? ??? ? ? ??? ????? ?? ?? ??? ???? ??? ????? ??? ?? showName() ???? ??? ?? ????? new ???? ?? ??? ????????.
JavaScript ???
JavaScript? ?? ?? ????? JavaScript? ???? ???? ????.
JavaScript??? ???? ???? ??? ?? ?? ?? ??? ????? ????? ??? ???? ????.
JavaScript? ??? ??? ?? ????? ?????.
JavaScript for...in ??
JavaScript for...in ?? ??? ??? ?????.
Syntax
for(??? ??){?? ???...}
??: for...in ??? ?? ??? ? ??? ?? ? ? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <button onclick="myFunction()">點擊這里</button> <p id="demo"></p> <script> function myFunction(){ var x; var txt=""; var person={fname:"Bill",age:56}; for (x in person){ txt=txt + person[x]; } document.getElementById("demo").innerHTML=txt; } </script> </body> </html>