The ABC of Programming Flashcards
What is a script and how do I create one?
A script is a series of instructions that the computer can follow in order to achieve a goal.
Each time the script runs, it might only use a subset of all the instructions.
Computers approach tasks in a different way than humans, so your instructions must let the computer solve the task programmatically.
To appreach writing a script, break down your goal into a series of tasks and then work out step needed to complete that task (a flowchart can help).
Object
Each physical thing in the world can be represented as an object. Each object can have its own properties, events, and methods.
Each property has a name and a value
Property
Object characteristics comprised of name/value pairs. Each of these pairs tell you something each individual instance of the object.
Event
Programmers choose which events they respond to. When a specific event happens, that event can be used to trigger a specific section of the code.
Scripts often use different events to trigger different types of functionality.
So a script will state which events the programmer wants to respond to, and what part of the script should be run when each of those events occur.
Method
Methods represent things people need to do with objects. They can retrieve or update the values of an object’s properties.
The code for a method can contain lots of instructions that together represent one task.
Window Object
The browser represents each window or tab using a window object. The location property of the window object will tell you the URL of the current page.
Document Object
The current web page loaded into each window is modelled using a document object.
The title property of the document object tells you what is between the opening (title) and closing (/title) tag for that web page, and the lastModified property of the document object tells you the date this page was last updated.
Like other objects that represent real-world things, the document object has: properties, methods, and events.
JavaScript Interpreter
When you use JavaScript in the browser, there is a part of the browser that is called an interpreter (or scripting engine).
The interpreter takes your instructions (in JavaScript) and translates them into instructions that browser can use to achieve the tasks you want it to perform.
In an interpreted programming language, like JavaScript each line of code is translated one-by-one as the script is run.
How do computers fit in with the world around them?
Computers create models of the world using data.
The models use objects to represent physical things. Objects can have: properties that tell us about the object; methods that perform tasks using the properties of that object; events which are triggered when a user interacts with the computer.
Programmers can write code to say “When this event occurs, run that code.”
Web browsers use HTML markup to create a model of the web page. Each element creates its own node (which is a kind of object).
To make web pages interactive, you write code that uses the browser’s model of the web page.
Calling a Method of an Object
document.write(“Good afternoon!”)
window.object.method.parameter = womp
The document object represents the entire web page. All web browsers implement this object, and you can use it just by giving its name.
The document object has several methods and properties. They are known as members of that object. You can access the members of an object using a dot between the object name and the member you want to access. It is called a member operator.
The write() method of the document object allows new content to be written into the page where the element sits.
Whenever a method requires some information in order to work, the data is given inside the parentheses. Each piece of information is called a parameter of the method. In this case, the write() method needs to know what to write into the page.
How do I write a script for a web page?
It is best to keep JavaScript code in its own JavaScript file. JavaScript files are text files (like HTML pages and CSS style sheets), but they have the .js extension.
The HTML
element is used in HTML pages to tell the browser to load the JavaScript file (rather like the <link></link> element can be used to load a CSS file).</p>
<p>If you view the source code of the page in the browser, the JavaScript will not have changed the HTML, because the script works with the model of the web page that the browser has created.</p>
</script>