CS174
Chris Pollett
Nov 2, 2020
function swap(i, j, a)
{
var tmp=a[i]; /* explicitly defined variables
have scope within the function
if I had declared the variable
implicitly it would have global scope */
a[i] = a[j]; a[j] = tmp;
}
swap(10, 5, b);
var c = swap;So could call:
c(10, 5, b);
function swap()
{
var i = this.arguments[0], j=this.arguments[1], a=this.arguments[2];
//same code as before
}
swap = function(i, j, a) {
/* same code as before*/
}
function car(new_make, new_model, new_year)
{
this.make = new_make;
this.model = new_model;
this.year = new_year;
}
my_car = new car("Ford", "Contour SVT", "2000");
function display_car()
{
document.write("Make:", this.make, "<br />");
document.write("Model:", this.model, "<br />");
document.write("Year:", this.year, "<br />");
}
function car(new_make, new_model, new_year)
{
//same as before
this.display = display_car;
}
function car(new_make, new_model, new_year)
{
//same as before
}
car.prototype.display = display_car;
function car(new_make, new_model, new_year)
{
//same as before
car.prototype.display = function()
{
document.write("Make:", this.make, "<br />");
document.write("Model:", this.model, "<br />");
document.write("Year:", this.year, "<br />");
}
}
function A() {
}
A.prototype.foo = function() {
alert("foo");
}
function B() {
}
B.prototype = new A();
/*
we just made B's prototype an instance of an A function object
which has its own prototype. So property look up for B,
looks within its instance, then within it prototype,
which is an instance of A, to see if it is a property of A,
then it looks at A's prototype to see if the property is there.
As A's prototype is an Object, it finally looks at Object's
prototype to see if the property is there.
*/
B.prototype.goo = function() {
alert("goo");
}
a = new A();
a.foo(); // alert with foo in it
b = new B();
b.foo(); // alert with foo in it
b.goo(); // alert with goo in it
a.goo(); // type error goo is not a function of A
var b = 10; // scope window variable object
function foo () { // same as window.foo
alert(typeof b); /* b can be used in foo.
this will say number */
}
foo();
var b = 10; // scope window variable object
function foo () {
alert(typeof b); /* the var b below is hoisted,
b property of this function used
which is currently undefined, so
outputs undefined*/
var b = "hello";
alert(typeof b); // outputs string
}
foo();
function checkingLetHoisting() {
console.log(foo); // ReferenceError
let foo = "blah";
console.log(foo); // blah
}
var object_we_are_making = (function () {
// private variables
return {
// public data and methods
}
}());
var employee = (function() {
var salary = 20000;
return {
name: "John Smith",
getSalary : function () {
return salary;
},
setSalary : function (sal) {
salary = sal;
}
}
}());
alert(employee.name); // John Smith
alert(employee.getSalary()); // 20000
employee.setSalary(100000);
alert(employee.getSalary()); // 100000
alert(employee.salary); // undefined
function Employee(name, sal) {
var salary; // scope within constructor
this.name = name;
salary = sal;
this.getSalary = function () {
return salary;
}
this.setSalary = function (sal) {
salary = sal;
}
}
var emp1 = new Employee("John", 100000);
var emp2 = new Employee("Sally", 150000);
alert(emp1.name); // John
alert(emp2.name); // Sally
alert(emp1.getSalary()); // 100000
emp2.setSalary(200000);
alert(emp2.getSalary()); // 200000
alert(emp1.getSalary()); // 100000
alert(emp1.salary); // undefined
class MyClass
{
contructor(arg1, arg2,...)
{
// code
this.foo = arg1; //an example of setting a field
}
//getter methods...
get myGetterFunction(arg1, ...)
{
// code
}
// a bunch or instance methods...
myMethod1(arg1, ...)
{
// code
}
// a bunch or static methods...
static myStaticMethod1(arg1, ...)
{
// code
}
}
class B extends A
{
//code
//can refer to parent in child using super keyword. I.e., super.foo()
}
//my_module.js
function hideme() {
return "from hideme";
}
export function showme() {
return showme();
}
export class goo
{
}
//another syntax for this is
function foo1()
{
}
function foo2()
{
}
export {foo1, foo2}
<script type="module" src="my_module.js" >
<script type="module"> // some code </script>
<script type="module" src="my_module.js" > <script type="module" src="my_module.js" >would have the same effect as just a single copy of the above line.
var x = 3.14; // if this was the first occurence of xrather than the more lazy
x = 3.14;
//my_second_module.js
import {showme} from "./my_module.js";
// I can now call showme()
import "some_other_module.js" //executes module only for side-effects
import * as ModuleName from "./module_name.js" //import everything with a name
/* if module_name.js exports foo(),
I could then use this function as ModuleName.foo();
*/
var str = "Rabbits are furry"; var position = str.search(/bits/); /* returns position of first occurrence */
/yx{5}z/ matches yxxxxxz
/Apple/i would match APPLE, aPple and apple.
var str="Fred, Freddie, Frederica"; str.replace(/Fre/g, "Boyd");notice we use g to replace all occurrences.
var str= "3 and 4"; var matches = str.match(/\d/g); //returns [3, 4]
var str="grapes:apples:oranges"
var fruit = str.split(":"); // [grapes, apples,oranges]