JavaScript基础 - 语言特性
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" ); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南