Jump to content

Still trying to learn javascript.


Drycodez

Recommended Posts

Parentheses.They exist as a place to put parameters. A parameter is a variable where you can pass data to a function for its own private use. Even if data is never passed to a function, the parentheses are required in the function definition and any calls to the function. So:

function my_func (my_var) {   alert(my_var);}my_func("hello");

In this snippet, you pass the string "hello" to my_func, and the variable my_var is assigned the value "hello".

Link to comment
Share on other sites

They exist as a place to put parameters. A parameter is a variable where you can pass data to a function for its own private use.
Well, I had posted a topic about the same, "what is Parameter?"Well at that time someone told me, Parameter is something which is required by something...? i don't exactly remember what was posted as answer...But does Parameter differ from language to language?
Link to comment
Share on other sites

There are a few languages that use the word "parameter" in a different way. JavaScript and PHP do use the word in the way I explained it.I can't tell yet if you understand the concept now. Do you need more explanation?

Link to comment
Share on other sites

A function can be defined with parameters or without parameters.without parameters:

function func_1 (){   alert("This function has no parameters");}func_1();

The first 4 lines are the function definition. The first line of a function definition requires:1. the word "function"2. the name of the function (in this case, "func_1")3. a set of parentheses.In line 5, we call (execute) the function. To call a function, write the name of the function followed by the set of parentheses. Because func_1 has no parameters, both sets of parentheses are empty. But they are still required.with parameters:

function func_2 (my_parameter){   alert(my_parameter);}var myString = "This function uses a parameter";func_2(myString);func_2("Hello!");

func_2 is a lot like func_1. The difference is that we put a parameter between the parentheses. A parameter can have any name you like. In func_2, we call the parameter "my_parameter". When you name a parameter in line 1 of the function definition, the parameter can be used inside the function the same way any variable can be used. func_2 simply alerts the value of my_parameter. In a more complicated function, it could do many different things with my_parameter.When you call a function that requires a parameter, the function call must send the function some data. Put the data inside the parentheses. This way, the function call looks a lot like line 1 of the function definition.When the function begins to execute, it assigns the data to the parameter. In func_2, the first call (line 6) passes the string "This function uses a parameter". func_2 assigns this string to my_parameter. func_2 will now alert the string "This function uses a parameter". In the second call (line 7) "Hello!" is passed to func_2. my_parameter now has the value "Hello!" When func_2 executes, it will alert the string "Hello!".Data can be passed to a function in 2 ways. You can pass it in a variable (lines 5-6) or you can pass literal data (line 7). Literal data means you pass a string or a number or something that is NOT in a variable.Notice that alert() is a built-in function. It works the same way. You can pass it a variable or literal data. We cannot see the function definition of alert() because it is built-in. But we know it works the same way. The function definition of alert() must contain a parameter. When we pass alert() some data, the value of that data gets assigned to the parameter.I hope this helps.

Link to comment
Share on other sites

Deirdre's Dad explained how it fits together, but in case you're having problem keeping track of the term, here's one way to keep it in perspective (having each term derived out of the other ones; Don't skip a term in this list!):1. Byte - a sequence of eight 1's and 0's.2. Data - one or more bytes.3. Value - A certain representation of "Data" suitable for display in programming code.4. (Data) type - a description of how a "Data" turns into a "Value" and vice-versa. An integer data type in the languages C or PHP for example means the data is 4 bytes long and the binary number is literally turned into a decimal notation or vice-versa (e.g. the bytes 00000000 00000000 00000000 00000001 are represented as 1).5. String - a specific data type in which bytes represent text in some fashion. All of this very post you're reading is stored as one big string. The most typical kind of string is an ASCII null terminated string - the string ends as soon as there's a byte with the code 00000000, and each byte represents a "character" (a symbol, i.e. a letter, a number, punctuation, etc.).6. Variable - something which holds a value that can be changed with another value while the program is running.7. Constant - something which holds a value that can't be changed with another value while the program is running.8. Function - something that can be done. When in your code you want to do it, we say we "call" or "execute" or "invoke" the function (the terms are synonymous).9. Parameter - data which is being copied to a function at the moment it is called. This copy of the data is accessible within the function as a variable. This is probably where the most misleading part is... in the code sample

function func_2 (my_parameter){   alert(my_parameter);}

my_parameter is a parameter to the function func_2, but within func_2, you're using it in the exact same fashion you use a variable. In other words, the data of the call will be accessible at the my_parameter variable, which is also a parameter, because its value was supplied at the call. From the perspective of the alert() function, when you call it, the value of my_parameter is the value that will be copied to the first parameter of the alert() function.(confusing, I know... read it again and do some doodles if you have to...)10. Class - in the context of "classic" object oriented programming, a class is a bunch of variables and functions. It's useful in that you can have multiple copies of a class (each copy is called an "instance" of a class).11. Property - a variable within a class.12. Method - a function within a class.The situation with JavaScript is more difficult than with other languages (one more reason you shouldn't start with it) because it doesn't have classes. It has prototypes instead, and a prototype is a concept which is much harder for newbies to wrap their heads around than classes (which appear hard as it is). Heck, I still need to revisit references on prototypes in order to make the switch from classical to prototypical mindset... it doesn't come naturally.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...