javascript: memory, passing, and objectscs115/slides/w06-04-objectsvalidation.pdf · • javascript...

22
JavaScript: Memory, Passing, and Objects CS 115 Computing for the Socio-Techno Web Brian Brubach

Upload: others

Post on 17-Oct-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

JavaScript:Memory,Passing,andObjects

CS115ComputingfortheSocio-TechnoWebBrianBrubach

Page 2: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

JavaScript“Primitive”DataTypes

• string• number• booleanà trueorfalse• null• undefined• symbol

Page 3: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

JavaScript“Primitive”DataTypes

• Imagineavariableforaprimitivetypestoresthevalue(simplificationofwhatreallyhappens)

var x = “word”;

var y = 12;

someFunction(x, y);

• ThefunctionsomeFunction receivesthevalues“word”and12• LocalvariablesdefinedbyparametersinsomeFunction havethesevalues• Alteringthoselocalvariableswillnotaffecttheglobalvariablexandy

• JavaScriptuses“passbyvalue”• Valueofavariablepassestoafunction• Thealternative,“passbyreference”,wouldpassthevariablesthemselves

function someFunciton(hot, cold){var warm = hot + cold;return warm;

}

Page 4: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

Arrays(andOtherObjects)

• Storedandhandleddifferentlythanprimitives• Imaginethevariablestoresonlytheaddress ofwherethearrayisactuallystored

var z = [“dog”, “cat”, “bee”];

document.write(z[1]); // Prints “cat”

document.write(z.length) // Prints “3”

• Supposezstorestheaddressofablockinmemory• Canimaginez=<someAddress>

“dog” “cat” “bee”0 1 2

3length

Location:<someAddress>Blockofmemorystoringthe

arraythatzpointstoorrefersto

Page 5: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

PassingArrays(andOtherObjects)intoFunctions

var z = [“dog”, “cat”, “bee”]; // stored at <someAddress>

someFunction(z);

• ThefunctionsomeFunction receivestheaddress<someAddress>of• LocalvariabledefinedbyparameterinsomeFunction hastheaddress<someAddress>

• someFunction cannotchangewhichaddresszpointsto• someFunction canchangethearrayatthataddress

// Change z to [“dog”, praying mantis”, “bee”]

function someFunction(animals) {

animals[1] = “praying mantis”;

}

Page 6: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

Null

• Representsnovalue• Representsnoaddress• Canassignit

var a = null;

Page 7: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

NullversusUndefined

• null• Cndicates novalue• Canbereturnedbyfunctions

• undefined• Valueassociatedwithuninitializedvariables• var x;• Whenafunctionthatisexpectedtoreturnavaluedoesnotreturnone(IMPORTANTcase)

• Valueassociatedwithobjectpropertiesthatdonotexist

• ==considersnullandundefinedequal• ===considersnullandundefineddifferent

• Rememberusing===and!==isbestpractice

Page 8: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

NaN

• NaNà Not-A-Number(SameasNumber.NaN)• Unequaltoanynumberincludingitself• UseisNaN functionà determines(returnstrueorfalse)whetheranargumentisnotanumber.Itattemptstoconverttheargumenttoanumber

• Thefollowingcomparisonsreturnfalse• NaN ==NaN• NaN ===NaN

• Theopposite:!isNaN()allowustodeterminewhetheranexpressionisanumber

• Notice:isNaN(20)à False• YoumaywanttowriteafunctioncallisNumber thatreturns!isNaN(x)

Page 9: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

WebDataValidationandMoreonSources

Page 10: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

Moreonsources

• Beawareofnon-peer-reviewedvenues• Includespreprintslikearxiv

• Googletoseeifjournalisreputable• Citetheconference/journal,notthepublisher• Examplesofpublishersorsearchtools

• WileyOnlineLibrary• SpringerLink• sciencedirect• Sagepublications• Googlescholar• ACMDigitalLibrary

Page 11: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

IronicWikipediaPage

• https://en.wikipedia.org/wiki/Data_validation (accessed4-25-19)

Page 12: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

Webliteracyresource

• https://webliteracy.pressbooks.com/front-matter/web-strategies-for-student-fact-checkers/

Page 13: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

• Somecontentfromdesigner/developerJohnBrieger’s blogpost:http://johnbrieger.com/blog/?p=321 (accessed4-25-19)

A TaleoftheBoardGameRisingSunandKōtahi

Page 14: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

ATaleoftheBoardGameRisingSunandKōtahi

• ThemeofgameisJapaneseMythology• Designer’spriorgamewasaboutNorseMythology

• Verysuccessfulgamefrompubliclytradedcompany• Kickstartercampaignselling30k+copiesandrevenueover$4million• SoldtensofthousandsmoreaftertheKickstarter• AvailableatBoard&Brewnextdoor

• April4th,2017à Kickstarterends(expecteddeliveryin2018)• January21st,2018à Afanasks,“WhatisKōtahi?”

Page 15: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

WhatisKōtahi?

• TheonlyinformationtobefoundisontheWikipediapage"ListoflegendarycreaturesfromJapan”

• AccordingtoMaoriDictionary,Kotahimeans“one”orasenseoftogetherness• ButtheMaoripeopleliveinNewZealandnotJapan

• Someonesearchedtheotherunfamiliarwords“ManawaBradford”…

Page 16: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

WhatisKōtahi?

• Kotahi-ManawaBradfordisafarmerinNewZealand• Friendsmakefunofhimforbeinghairyandgettingangrywhileplayinggames• OnefriendmadeafakeWikipediaeditasajoke• ThedesignerofRisingSun(whoisbrilliantandhard-working)usedWikipediaasreferencewithoutchecking

Page 17: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

AnArtist’sRenderingofKōtahi

Page 18: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

• AppearedonNewZealandnews• Thecompanysenthimafreeautographedgame

• Hehasaplasticfigurineofhisfriends’insidejokeabouthim• Moral:Don’ttrustWikipediaforresearch,butdouseitforjokes?

OutcomefortheRealKotahi

Page 19: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

CheckingWikipediaEdits

• CancheckaWikipediapage’sedithistorybyclicking“Viewhistory”inthetoprightcornerofthepage• Showsyouwhomadeedits,whatthechangeswere,andwhen

• HereisoneeditforKōtahi• https://en.wikipedia.org/w/index.php?title=List_of_legendary_creatures_from_Japan&diff=next&oldid=738160684

• Theychanged“getsreallyangry”to“getsengulfedinrage”

• Warning:SomebogusWikipediaeditsaredisgusting,hateful,offensive,etc.• Luckily,abot,ClueBot NG,catchesmanyoftheseanddeletesthemfairlyquickly,buttheyareloggedintheedithistory

Page 20: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

CriteriatoEvaluateWebData

• Authorship• Whowrotethedocument?• Doyourecognizetheauthor(e.g.,someoneinyourfield)?• Isthedocumentlinkedtoadocumentyoutrust?• Isbiographicalinformationprovided?• Istheauthorreferredtobyatrustauthority(persons)?

• PublishingEntity• Anyorganizationnameprovidedinthedocument?• Canyoucontactthewebmaster?• Anydocumentparts(headers,images,etc.)associatedwithanorganization?• IstheURLassociatedwithanorganizationyoutrust?• Canyouverifytheidentityoftheserverviawhois serversordnslookup?• Howaretheyfundedorwhatistheirrevenuemodel?

• Pointofview– Examinewhoisprovidingtheinformationandwhatmightbetheirpointofview• Isitpartofanorganizationwithaphilosophicalorpoliticalagenda?

Page 21: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

CriteriatoEvaluateWebData

• Contextauthorsituatesthework• Authordisplaysknowledgeorsources,theories,techniques• Documentincludesabibliography

• Accuracy• Documentreliesonsourceslistedinabibliography• Backgroundinformationusedcanbeverifiedforaccuracy• Methodologypresentedisappropriateforthetopicandallowsforstudyduplication

• Currency(TimelinessofInformation)• Keepinmindthatforsomedocumentsthisisnotanissue• Documentreferstoclearlydatedinformation• Documentincludesapublicationdate

• Guidetoevaluatingwebpages(sourcesformostofthiscontent)• https://guides.library.jhu.edu/evaluate/internet-resources• http://guides.lib.berkeley.edu/evaluating-resources

Page 22: JavaScript: Memory, Passing, and Objectscs115/slides/W06-04-ObjectsValidation.pdf · • JavaScript uses “pass by value” ... • Friends make fun of him for being hairy and getting

WebSiteValidation(whois servers)

• Importanceoffindingownerofwebsite• http://www.dhmo.org/• Googledhmo.org authorTomWay

• Whois servers databasesthatkeeptrackofownersofdomains• https://www.whois.com/whois/

• Typedomain:umd.edu• YougetlotsofvalidcontactinfobecauseUMDislegit