Intro Flashcards
JavaScript
- a scripting language meaning that it does not need compile
- programming language that provides instructions to a web browser
Ways to include JavaScript in your page
alert(“Hello Everybody”)
-use the script tags and place in the JS methods.
or use the source attribute as long as js file is in the same directory.
src= “alert.js”
Datatypes
-specialized form of information.
-string: sequence of characters
-number: integer and floats
-boolean
-object: represents a group of key/value pairs
-date: represents date/time
-array: represents a collection of data
var arr= [1,’232’,582]
- undefined: value not yet defined
- null; empty value
variable
a temporary storage unit that stores data
ex: var variableName;
JS Naming rules
- follow camelCase convention (start w lower case)
- names must begin w letter, _, or $
- numbers and Unicode escape sequence can be used after the first character
- names are case sensitive
variable value assignment
-use = sign then brackets
examples:
var name = "Jennifer"; var num = 3;
ex: var carSpecs = { numWheels: 4, engine: 'V6', interior: 'cloth' };
Single and multiline comments
single - //
multiline - /*comment */
to print to console
console.log(“”);
creating function
group of block statements
use function keyword:
function talk() { alert("Hello!"); }
talk();
Hoisting
- a variable declared without using the var keyword, makes it available outside of the function
- hoisting is the process of assigning loose variables to the global window object
Creating objects using literal notation
objects are key/value pairs
ex: var Person = { numWheels: 4, property: value, property: value };
OR use objects constructor
ex: var Person = new Object();
dot notation
access properties of an object using a dot
Person.name = “Adam”;
array notation
access properties using brackets
ex: Person[“name”] = “Adam”;
defining a method
a method that is assigned as property of an object
var Person = { givenName: function { //.... } }
this keyword
this keyword refers to the current executing context but outside of it, it refers to the global window object
HTML DOM - Document Object Model
**It is the programming interface that allows you to retrieve and manipulate elements of a webpage; otherwise referred to as document. **
**DOM, provides a tree-like structure or representation of all of the elements and their corresponding content of a webpage. Each element is referred to as a node. **
A browser’s DOM also provides methods for adding/removing/editing content. It provides what we call an API, which stands for Application Programming Interface
API - Application Programming Interface
An API is like a recipe book that lists several methods used for a particular purpose. In our case, we use the API to manage elements on a webpage as well as manage user interactions such as button clicks.
Accessing the DOM methods
specific methods that are used to retrieve elements or arrays of elements from the webpage document.
getElementById - will retrieve the first matched element with the specified id.
var doc = document.getElementById(“myID”)
getElementsByTagName – will retrieve an array of elements (list?) of the specified tag.
For example, you can retrieve all paragraphs on the page by passing in “p”, which corresponds to the paragraph tag.
getElementsByClassName – will retrieve an array (list?)of elements that have the specified class.
Changing HTML : innerHTML
- text-based elements have the property innerHTML that you can access to change the contained text
element. innerHTML = “new content”;
Changing HTML : setAttribute()
- attributes can be set with this function
element. setAttribute(“name”, value);
Changing CSS : use style property
- use style property to change the CSS of an element
element. style.property = value;
Adding/Deleting elements from a page/document methods
createElement()
creates an element of the specified tag
appendChild()
adds a child element to the one specified
removeChild()
deletes a child element of the one specified
Event Handling - events definition
-events are actions that occurs, initiated by user or within browser
ex:
user clicking their mouse
Event handlers
event handlers/listeners are
registered with a particular event and notify when an event occurs
listeners can be added to DOM elements