JavaScript基础 - 语言特性

简介: JavaScript是基于函数的语言,一切都是对象,但又比较特殊 引用 引用是指向实际对象的一个指针和C/C++的指针一样,C#和java的对象也是引用传递   函数重载 // A simple function for sending a message functio...

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" );

相关文章
|
22天前
|
Rust JavaScript 前端开发
Rust! 无VDom! 尤雨溪解析 Vue.js 2024 新特性
Rust! 无VDom! 尤雨溪解析 Vue.js 2024 新特性
|
23天前
|
Web App开发 前端开发 JavaScript
[译] JavaScript ES2021 中激动人心的特性
[译] JavaScript ES2021 中激动人心的特性
|
25天前
|
前端开发 JavaScript 开发者
翻天覆地!ES6+新特性大爆发,揭秘JavaScript代码的惊人蜕变!
【8月更文挑战第27天】自ES6标准发布以来,JavaScript新增的特性极大地提升了编程效率并简化了代码。本文重点介绍了五大特性:函数默认参数简化、模板字符串的强大功能、解构赋值的便捷性、箭头函数的简洁之美。这些特性不仅使代码更加简洁、易读,还解决了传统写法中的诸多问题。通过学习和应用这些新特性,开发者可以编写出更高效、更优雅的代码,以应对日益复杂的编程挑战。
40 2
|
26天前
|
JavaScript 前端开发 安全
JS 混淆解析:JS 压缩混淆原理、OB 混淆特性、OB 混淆JS、混淆突破实战
JS 混淆解析:JS 压缩混淆原理、OB 混淆特性、OB 混淆JS、混淆突破实战
36 2
|
1月前
|
JavaScript 前端开发 API
JavaScript特性检测
JavaScript特性检测
|
22天前
|
自然语言处理 JavaScript 前端开发
【走向世界】Vue.js国际化:打造无国界应用,让爱与信息跨越语言的边界!
【8月更文挑战第30天】本文详细介绍了Vue.js中实现国际化的多种方法及最佳实践。通过使用`vue-i18n`等第三方库,开发者能够轻松地为应用添加多语言支持,优化用户体验并扩大市场覆盖范围。文章涵盖从基本配置、动态加载语言包到考虑文化差异等方面的内容,帮助读者构建真正全球化且无缝多语言体验的应用程序。
41 0
|
1月前
|
Web App开发 JavaScript 前端开发
Node.js与Go语言的对比?
【8月更文挑战第4天】Node.js与Go语言的对比?
168 3
|
29天前
|
自然语言处理 前端开发 JavaScript
前端进阶必读:JS闭包深度解析,掌握这一特性,你的代码将焕然一新!
【8月更文挑战第23天】闭包是JavaScript的一项高级功能,让函数能够访问和操作外部函数作用域中的变量。本文深入解析闭包概念、组成及应用场景。闭包由函数及其词法环境构成,通过在一个函数内定义另一个函数来创建。它有助于封装私有变量、维持状态和动态生成函数。然而,不当使用闭包可能导致内存泄漏或性能问题。掌握闭包对于实现模块化代码和成为优秀前端开发者至关重要。
30 0
|
30天前
|
JavaScript 前端开发 UED
探索JavaScript的历史:网络需求初现、语言创立与标准化的旅程
探索JavaScript的历史:网络需求初现、语言创立与标准化的旅程
|
2月前
|
机器学习/深度学习 数据采集 前端开发
网络爬虫开发:JavaScript与Python特性的小差异
我们以前写JavaScript的代码时,在遇到了发送请求时,都是需要去await的。 但是为什么Python代码不需要这样做呢? 这就是因为JavaScript是异步的,Python是同步的。 JavaScript就需要使用关键词await将异步代码块变为同步代码。