JavaScript基礎 – 語言特性

來源:互聯網
上載者:User

JavaScript是基於函數的語言,一切都是對象,但又比較特殊

引用

引用是指向實際對象的一個指標和C/C++的指標一樣,C#和java的對象也是引用傳遞

 

函數重載

// A simple function for sending a message

function sendMessage( msg, obj ) {

    // If both a message and an object are provided

    if ( arguments.length == 2 )

        // Send the message to the object

        obj.alert( msg );

 

    // Otherwise, assume that only a message was provided

    else

        // So just display the default error message

        alert( msg );

}

 

// Both of these function calls work

sendMessage( "Hello, World!" );

sendMessage( "How are you?", window );

範圍

範圍是由函數劃分的,而不是塊(Block)劃分,和其他的語言不一樣的地方

// Set a global variable, foo, equal to test

var foo = "test";

 

// Within an if block

if ( true ) {

    // Set foo equal to 'new test'

    // NOTE: This is still within the global scope!

    var foo = "new test";

}

 

// As we can see here, as foo is now equal to 'new test'

alert( foo == "new test" );

 

// Create a function that will modify the variable foo

function test() {

    var foo = "old test";

}

 

// However, when called, 'foo' remains within the scope

// of the function

test();

 

// Which is confirmed, as foo is still equal to 'new test'

alert( foo == "new test" );

 

JavaScript全域範圍就是window對象

// A globally-scope variable, containing the string 'test'

var test = "test";

 

// You'll notice that our 'global' variable and the the

// property of the the window object are identical

alert( window.test == test );

 

// A function in which the value of foo is set

function test() {

    foo = "test";

}

 

// Call the function to set the value of foo

test();

 

// We see that foo is now globally scoped

alert( window.foo == "test" );

閉包

閉包意味著內層的函數可以引用存在於包含它的函數內的變數,即使外層函數的執行已經終止

詳細參考:http://jibbering.com/faq/faq_notes/closures.html

不用產生全域函數:

// Create a new anonymous function, to use as a wrapper

(function(){

    // The variable that would, normally, be global

    var msg = "Thanks for visiting!";

 

    // Binding a new function to a global object

    window.onunload = function(){

        // Which uses the 'hidden' variable

        alert( msg );

    };

 

// Close off the anonymous function and execute it

}());

上下文

this 這個功能在JavaScript中充分發揮

 

var obj = {

    yes: function(){

        // this == obj

        this.val = true;

    },

    no: function(){

        this.val = false;

    }

};

 

// We see that there is no val property in the 'obj' object

alert( obj.val == null );

 

// We run the yes function and it changes the val property

// associated with the 'obj' object

obj.yes();

alert( obj.val == true );

 

// However, we now point window.no to the obj.no method and run it

window.no = obj.no;

window.no();

 

// This results in the obj object staying the same (as the context was

// switched to the window object)

alert( obj.val == true );

// and window val property getting updated.

alert( window.val == false );

 

以上處理有些不太同一理解,JavaScript提供了call和apply方法實現這個功能:

// A simple that sets the color style of its context

function changeColor( color ) {

    this.style.color = color;

}

// Calling it on the window object, which fails, since it doesn't

// have a style object

changeColor( "white" );

 

// Find the element with an ID of main

var main = document.getElementById("main");

// Set its color to black, using the call method

// The call method sets the context with the first argument

// and passes all the other arguments as arguments to the function

changeColor.call( main, "black" );

 

// A function that sets the color on  the body element

function setBodyColor() {

    // The apply method sets the context to the body element

    // with the first argument, the second argument is an array

    // of arguments that gets passed to the function

    changeColor.apply( document.body, arguments );

}

// Set the background color of the body to black

setBodyColor( "black" );

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.