javascript
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| javascript [2021/06/05 17:20] – admin | javascript [2021/07/23 14:52] (current) – admin | ||
|---|---|---|---|
| Line 8: | Line 8: | ||
| ## Basic | ## Basic | ||
| + | |||
| + | ### Datatype | ||
| + | |||
| + | ``` | ||
| + | var length = 16; // Number | ||
| + | var lastName = " | ||
| + | var x = {firstName:" | ||
| + | ``` | ||
| ### Object | ### Object | ||
| Create an object with 3 properties: | Create an object with 3 properties: | ||
| - | ``` | + | ```javascript |
| var object = { | var object = { | ||
| ID: 1, | ID: 1, | ||
| " | " | ||
| - | | + | |
| + | math: 94 | ||
| + | physics:89 | ||
| } | } | ||
| - | }; | + | // When a function is used as an object property, it is called a method |
| + | sum: function(a, | ||
| + | return a+b; | ||
| + | } | ||
| + | } | ||
| ``` | ``` | ||
| - | Use dot ```.``` or bracket ```[]``` to access properties | + | Use dot ```.``` or bracket ```[]``` to access |
| - | ``` | + | ```javascript |
| object.ID = object.ID + 1; | object.ID = object.ID + 1; | ||
| object[" | object[" | ||
| + | object.grade.math = 100; | ||
| + | // Method can be called in the same way | ||
| + | object.sum(1, | ||
| + | |||
| + | // Create new properties or methods in the same way | ||
| object[name + " | object[name + " | ||
| + | object.minus = function(a, | ||
| + | return a-b; | ||
| + | }; | ||
| ``` | ``` | ||
| - | Bracket can be used to create new properties | + | ### Question Mark "?" |
| + | Three use of question mark: [[https:// | ||
| ## jQuery | ## jQuery | ||
| Line 37: | Line 60: | ||
| A jQuery action() to be performed on the element(s)\\ | A jQuery action() to be performed on the element(s)\\ | ||
| - | + | By default, jQuery uses "\$" as a shortcut for " | |
| - | ### The $ mark | + | |
| - | By default, jQuery uses " | + | |
| - | + | ||
| - | Doing ````var $=function(){}```` is just defining a function called " | + | |
| Line 72: | Line 91: | ||
| ``` | ``` | ||
| - | The pseudo-code for that block is: | + | The pseudo-code for that block is: When the document object model ````$(document) is ready .ready()````, |
| - | + | ||
| - | When the document object model ````$(document) is ready .ready()````, | + | |
| ### Functions in jQuery | ### Functions in jQuery | ||
| - | **Example:** hide the current element | + | Example: hide the current element |
| ```javascript | ```javascript | ||
| $(this).hide() | $(this).hide() | ||
| Line 113: | Line 130: | ||
| ``` | ``` | ||
| - | #### Create your own functions (plugins) | + | #### jQuery |
| 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 129: | Line 146: | ||
| JavaScript statements are executed line by line. However, with effects, the next line of code can be run even though the effect is not finished. This can create errors. | JavaScript statements are executed line by line. However, with effects, the next line of code can be run even though the effect is not finished. This can create errors. | ||
| - | To prevent this, you can create a callback function. | + | To prevent this, you can create a callback function. A callback function is executed after the current effect is finished. Typical syntax: ```$(selector).hide(speed, |
| - | + | ||
| - | A callback function is executed after the current effect is finished. | + | |
| - | + | ||
| - | Typical syntax: ```$(selector).hide(speed, | + | |
| Example: | Example: | ||
| Line 150: | Line 163: | ||
| ```javascript | ```javascript | ||
| $("# | $("# | ||
| + | ``` | ||
| + | |||
| + | #### 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(" | ||
| + | }); | ||
| + | |||
| + | MyBlah(" | ||
| + | ``` | ||
| + | |||
| + | 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(" | ||
| + | }); | ||
| + | |||
| + | $.MyBlah(" | ||
| ``` | ``` | ||
javascript.1622884832.txt.gz · Last modified: (external edit)
