1.關於prototype:這裡prototype是javascript的一個特性,不是那個有名的prototype框架:
<script type="text/javascript">
var string="hello world";
try{
alert(string.phone());
}catch(e){alert(e);}
String.prototype.phone=function()
{
return "159-10957151";
}
alert(string.phone());
</script>
2.關於變量作用域,和IE,firefox對js的不同處理,這裡有幾個例子,有幾個是原來從別處看到的記的筆記,有的是我自己挖掘出來的.
2.1
<script type="text/javascript">
var deep_thought = {
the_answer: 42,
ask_question: function () {
return this.the_answer;
}
};
var the_meaning = deep_thought.ask_question();
alert(the_meaning);
</script>
2.2
<script type="text/javascript">
function test_this() {
return this;
}
var i_wonder_what_this_is = test_this();
alert(i_wonder_what_this_is);
// result: [object window];
</script>
2.3:
<script type="text/javascript">
function click_handler() {
alert(this); // 彈出 window 對象
}
</script>
...
<button id='thebutton' onclick='click_handler()'>Click me!</button>
2.4
<script type="text/javascript">
function click_handler(obj) {
alert(obj);
//result:[object HTMLButtonElement]
}
</script>
...
<button id='thebutton' onclick='click_handler(this)'>Click me!</button>
2.5
<button id='thebutton' onclick='click_handler(this)'>Click me!</button>
<script type="text/javascript">
function BigComputer(answer) {
this.the_answer = answer;
this.ask_question = function () {
alert(this.the_answer);
}
}
function addhandler() {
var deep_thought = new BigComputer(42),
the_button = document.getElementById('thebutton');
the_button.onclick = deep_thought.ask_question;
}
window.onload = addhandler;
//result [undefined]
</script>
...
2.6
<button id='thebutton' onclick='click_handler(this)'>Click me!</button>
<script type="text/javascript">
function BigComputer(answer) {
var self=this;
self.the_answer = answer;
self.ask_question = function () {
alert(self.the_answer);