CS174
Chris Pollett
Nov 7, 2022
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
Which of the following statements is true?
class MyClass { a_public_field; //public field declaration #a_private_field; // private field declaration notice # 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 } #myPrivateMethod1(arg1, ...) //notice # indicates private { // code } // one can also have generator methods preceded with * *getSides() { for (const some_var of this.some_iterable_field) { yield some_var; } } // a bunch or static methods... static myStaticMethod1(arg1, ...) { // code } }
let some_object = class { //class definition }
const p = new MyClass(); // ReferenceError class MyClass {}
class B extends A { //code //can refer to parent in child using super keyword. I.e., super.foo() }
function A(some_arg) { this.some_field = some_arg; } class B extends A { //code }
//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 occurrence 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]
<input type="button" name="turnItOn" />then it could be accessed with document.forms[0].elements[0].
<input type="button" id="turnItOn" name="turnItOn"/>
button = document.getElementById("turnItOn");
myForm = document.getElementById("bob"); numButtons = myForm.vehicles.length; // if we want to we could cycle over this array for values. for( i =0 ; i < numButtons; i++) { oneVehicle = myForm.vehicles[i]. // do something }