codecamp

JavaScript building blocks

预备知识

再开始这部分模块之前, 你应该熟悉基本HTML and css, 还有看完我们之前的模块, JavaScript first steps.

注解: 如果你在用电脑/平板/其他设备你没有能力来创建自己的文件,你可以试试在线编辑例如 JSBinThimble.

指南

在代码中做决定 — 条件
在任何程序语言中, code needs to make decisions and carry out actions accordingly depending on diferent inputs. 例如, 在游戏中, 如果玩家的生命值是0,游戏就结束. 在天气app中, 如果在早上,则显示一个太阳升起的图片; 如果在晚上就显示星星和月亮. 在这篇文章里我们将探索条件语句结构是怎么在JS里面执行的.
循环
Sometimes you need a task done more than once in a row.例如,查看一整列的名字.在程序中, loops perform this job very well循环能非常好的处理好这个职责. Here we will look at loop structures in JavaScript.
函数 — 可重用的代码块
Another essential concept in coding is functions. Functions allow you to store a piece of code that does a single task inside a defined block, and then call that code whenever you need it using a single short command — rather than having to type out the same code multiple times. In this article we'll explore fundamental concepts behind functions such as basic syntax, how to invoke and define functions, scope, and parameters.
打造自己的函数
With most of the essential theory dealt with in the previous article, this article provides a practical experience. Here you'll get some practice with building up your own custom function. Along with way, we'll also explain some further useful details of dealing with functions.
函数返回值
There's one last essential concept for us to discuss in this course, to close our look at functions — return values. Some functions don't return a significant value after completion, but others do. It's important to understand what their values are, how to make use of them in your code, and how to make your own custom functions return useful values. 
事件介绍
Events are actions or occurrences that happen in the system you are programming, which the system tells you about so you can respond to them in some way if desired. For example if the user clicks a button on a webpage, you might want to respond to that action by displaying an information box. In this final article we will discuss some important concepts surrounding events, and look at how they work in browsers.

评估

以下评估将测试您对上述指南中所述的JavaScript基础知识的理解。

Image gallery
Now that we've looked at the fundamental building blocks of JavaScript, we'll test your knowledge of loops, functions, conditionals and events by building a fairly common item you'll see on a lot of websites — a JavaScript-powered image gallery.
Setting up your own test automation environment
Making decisions in your code — conditionals
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录
CSS

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }