最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript functions, how can I start beginning to understand them? - Stack Overflow

programmeradmin0浏览0评论

I totally understand that in order to learn javascript I need to know how functions work, I understand the basics of passing in parameters and then calling the function with the values to maybe add something together, etc. I've read countless articles about functions as well as books, etc., but I just dont get how they are used and when they should be used, etc., the more advanced functions that have maybe 4 parameters and are doing different calculations and returning various values that get fired back into the script just totally confuses me.

What I would like to know is first of all how can I overcome this confusion and also any words of wisdom you may have? I will also add that I have no prior programming experience and have spent the last 2 months frequently hitting my head off a brick wall as I just cant understand javascript.

I totally understand that in order to learn javascript I need to know how functions work, I understand the basics of passing in parameters and then calling the function with the values to maybe add something together, etc. I've read countless articles about functions as well as books, etc., but I just dont get how they are used and when they should be used, etc., the more advanced functions that have maybe 4 parameters and are doing different calculations and returning various values that get fired back into the script just totally confuses me.

What I would like to know is first of all how can I overcome this confusion and also any words of wisdom you may have? I will also add that I have no prior programming experience and have spent the last 2 months frequently hitting my head off a brick wall as I just cant understand javascript.

Share Improve this question edited Jul 27, 2011 at 9:54 jonsca 10.4k26 gold badges55 silver badges63 bronze badges asked Jul 27, 2011 at 9:41 stylerstyler 16.5k25 gold badges85 silver badges139 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 8

Functions in programming are similar to functions in math. They take some input and produce an output (well, sometimes they don't, but they do something). They are great to organize your code, encapsulate functionality and to avoid writing the same code at different places (DRY).

Functions taking more parameters are not necessarily more complex.

Have a look at

  • Wikipedia - Function(programming)
  • MDC - JavaScript Guide
  • especially MDC - JavaScript Guide: Functions
  • MDC - JavaScript Reference: Functions and function scope

Functions provide a way to segment repeated portions of your code so that you don't have to write the same functionality over and over again.

If you're after a non-programming analogy, you can think about it in business terms:

Imagine you have to print, collate, hole-punch and bind a document. First time around, you do it yourself. Then next time, you have to do it yourself again... and again... So, what can you do? You can hire an admin assistant (let's call him Bill) to do that for you. Then, the next time you have to print, collate, hole-punch and bind a document, you can just tell Bill which document to print (a single parameter) and he'll do it for you and bring the document back to you when it's finished.

In this case, Bill's a function that does some work and returns something (or, at least, an example of one!)

Now, imagine you want to send a letter to someone to chase a payment. Of course, you could type the letter and post it yourself, but wouldn't it be easier if someone else did it for you? Let's call her Jane. You can tell Jane which company to send the letter to and how much they owe (the parameters) and she'll go off, type it and send it. You don't necessarily need to know whether she's done it or not, because you trust her to get the job done.

In this case, Jane's a function that doesn't return anything but still does some work.

There are two things you must understand, in my opinion:

  • the DOM structure, which is actually what you manipulate using JavaScript
  • the JavaScript language (or even programming altogether)

I'd go for a basic tutorial like this one: http://www.lynda.com/JavaScript-tutorials/Essential-Training-2011/81266-2.html

And the most important part is not just reading, doing a lot of examples and exercises. If you don't code, you can't get the hang of it.

IMO besides high-level understanding of functions, how they're implemented and how they work, it is essential to understand low-level basics of (javascript) programming.

It doesn't really matter if you decide to learn that with javascript, since the idea behind if(){} / else{}, for() etc. is basically the same everywhere. It is much easier to understand concepts that way, since you will actually know what they do when you look into the code, instead of taking the developer's word on what it does

Start by reading "JavaScript: The Good Parts" and watching Doug Crockford videos at YUI Theater.

Then keep writing code.

Functions are mostly used to avoid duplicated code and simplify repeated tasks. They also help to compress the code you write and make it more readable.

For me your question seems to be like you'r standing in front of a pile of LEGO (javascript/programming) and asking what to do with the red bricks (functions).

The best way from there is to find something you want to do with your LEGO. Look for simple things that other people have done (tutorials) and find something that you want to do. Then try to do it and see where the red bricks fit in.

As mentioned by others the best way is to practice it, try it your self, have a goal.

发布评论

评论列表(0)

  1. 暂无评论