This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| javascript [2021/06/05 17:36] admin [Object] | javascript [2021/07/23 14:52] (current) admin | ||
|---|---|---|---|
| Line 8: | Line 8: | ||
| ## Basic | ## Basic | ||
| + | |||
| + | ### Datatype | ||
| + | |||
| + | ``` | ||
| + | var length = 16; // Number | ||
| + | var lastName = "Johnson";  // String | ||
| + | var x = {firstName:"John", lastName:"Doe"};  // Object | ||
| + | ``` | ||
| ### Object | ### Object | ||
| Line 41: | Line 49: | ||
| ``` | ``` | ||
| + | ### Question Mark "?" | ||
| + | Three use of question mark: [[https://www.freecodecamp.org/news/how-the-question-mark-works-in-javascript/|Link]] | ||
| ## jQuery | ## jQuery | ||
| Line 121: | Line 130: | ||
| ``` | ``` | ||
| - | #### Create your own functions (plugins) | + | #### jQuery plugins | 
| Example: to create a plugin that makes text within a set of retrieved elements green. All we have to do is add a function called greenify to ```$.fn``` and it will be available just like any other jQuery object method. | Example: to create a plugin that makes text within a set of retrieved elements green. All we have to do is add a function called greenify to ```$.fn``` and it will be available just like any other jQuery object method. | ||
| Line 154: | Line 163: | ||
| ```javascript | ```javascript | ||
| $("#p1").css("color", "red").slideUp(2000).slideDown(2000); | $("#p1").css("color", "red").slideUp(2000).slideDown(2000); | ||
| + | ``` | ||
| + | |||
| + | #### Normal javascript function in jQuery? | ||
| + | You might want to just have a function in jQuery to use the query features. However it could not be called outside of the jQuery namespace. Here's an example: | ||
| + | ```javascript | ||
| + | $(document).ready( function () { | ||
| + | var MyBlah = function(blah) { alert(blah);  }; | ||
| + | MyBlah("Hello this works") // Inside the anonymous function we are cool. | ||
| + | }); | ||
| + | |||
| + | MyBlah("Oops") //This throws a JavaScript error (MyBlah is not a function) | ||
| + | ``` | ||
| + | |||
| + | This is (sometimes) a desirable behavior since we do not pollute the global namespace, so if your function does not need to be called from other part of your code, this is the way to go. | ||
| + | |||
| + | Declaring it outside the anonymous function places it in the global namespace, and it's accessible from everywhere. | ||
| + | |||
| + | If you want to call it anywhere, use the `$.` namespace: | ||
| + | ```javascript | ||
| + | $(document).ready( function () { | ||
| + | $.MyBlah = function(blah) { alert(blah);  }; | ||
| + | MyBlah("Hello this works") // Inside the anonymous function we are cool. | ||
| + | }); | ||
| + | |||
| + | $.MyBlah("Oops") //This throws a JavaScript error (MyBlah is not a function) | ||
| ``` | ``` | ||