Bài viết nhỏ này trình bày các cách thức để khai báo, viết và gọi hàm trong ngôn ngữ lập trình JavaScript, như: hàm thông thường, hàm có giá trị trả về, hàm trong hàm, ... Vui lòng vào trang chi tiết để tìm hiểu thêm.
- Demo
- Phóng to
- Tải lại
- Cửa sổ mới
Miễn phí web hosting 1 năm đầu tại iPage
Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?
Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.
Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
Just a random, late night scribble for the boys and girls out there starting fresh with javascript and not understanding the power that you can harness with it. I've given this demonstration before, but it's always fun to post because someone might learn something new.
Anyway, you might be familiar with your usual function definition, let's say a function that sums 2 numbers:
function sum(a,b){ return a+b; } sum(2,5); // returns 7
That's fine and dandy... but now let's change this around a bit to better illustrate what a function is:
var pre = function(a,b){ return a+b; } sum(2,5); // returns 7
Yep, that's right... a function is just an object in javascript and the name of it is simply a variable. Now let's make this a bit more interesting by creating a function that returns a running sum:
function runningSum(start){ var sum = start; return function(a){ return sum+=a; } } var sum = runningSum(3); sum(2); // returns 5 sum(10); // returns 15
As you see, you can have a function return another function in javascript, and that function inherits the scope of the function that created it... in fact we could remove the temporary sum variable and get the same results:
function runningSum(start){ return function(a){ return start+=a; } } var sum = runningSum(3); sum(2); // returns 5 sum(10); // returns 15
now, lets introduce the arguments variable, which gives you access to an array of agu ments passed into the function. A revised sum function:
function sum(){ var sum = 0; for(var i = 0; i < arguments.length; i++){ sum += arguments[i]; } return sum; } sum(1,2,2,1); // returns six
The arguments object is a pretty damn cool object, if you want to look further, take a gander at the Mozilla documentation. Using the callee attribute of arguments (which returns the function instance itself), we can do something wicked:
var sum = 0; function add(a){ sum+=a; return arguments.callee; } add(1)(2)(3); alert(sum); // displays 6
I find this can come in pretty handy when you need an accumulator of some sort... try it, it works!
Also, you can assign the scope of this while using functions, which can be useful if, for example, you want to call a function where this might resolve to the local scope (think ajax callbacks):
function greeting(){ return "Hello " + this.name; } var person = {'name':'James'}; greeting.call(person); // returns "Hello James"
I also prefer to avoid polluting the global scope if I want a script that does something procedural when it's included, in that case I just create a function and execute it on the spot:
(function(){ var bleh = 3; alert(bleh+5); // alerts 8 })(); alert(bleh); // Reference Error: bleh is not defined
Finally, there's a handy map function on arrays. It's available in Mozilla, but not IE (afaik), however it is also available in jquery, so you can give it a try there.
[1,2,3,4,5].map(function(a){return a*a;}); // returns [1,4,9,16,25]
Overall, pretty basic stuff, but a nice refresher if you are unfamiliar.
- Lượt gửi (0)
- Mới
Save up to 630$ when buy new iPhone 15
GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project
https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools
chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language
Dall-E-OpenAI.com
Generate creative images automatically with AI
AIVideo-App.com
Render creative video automatically with AI
Thank you.. Phản hồi
hic Phản hồi
no pain no gain @ nhung Phản hồi