[PDF] [PDF] Functional and Object-Oriented Javascript - Washington

Functional Javascript with anonymous methods and methods-as-variables Objective Javascript with both A more realistic example: function removeEvens (a) { This is object-oriented programming, so where are my methods and fields ?



Previous PDF Next PDF





[PDF] JAVASCRIPT OBJECT-ORIENTED

The principles of object-oriented JavaScript / by Nicholas C Zakas pages cm JavaScript, you never need to write a class definition, import a package, or include a JavaScript Familiarity with Java, C#, or object-oriented programming in 



[PDF] Object-Oriented JavaScript

In this chapter, you'll learn about OOP (Object-Oriented Programming) and how it relates to downloaded in PDF format at http://www cristiandarie ro/downloads/ To ensure we a few examples of creating objects in JavaScript: // create a 



[PDF] Principles of Object-Oriented Programming in JavaScript - Leanpub

4 nov 2019 · This, quite simply, is the definition of an object • Aggregation - One object can reference another object • Inheritance - A newly created object has 



[PDF] Functional and Object-Oriented Javascript - Washington

Functional Javascript with anonymous methods and methods-as-variables Objective Javascript with both A more realistic example: function removeEvens (a) { This is object-oriented programming, so where are my methods and fields ?



[PDF] Creating and using JavaScript objects - IBM

What is this tutorial about? Object oriented programming (OOP) is a means for dividing a program into objects with predefined properties and behaviors, known as 



[PDF] JavaScript Objects Overview - Tutorialspoint

JavaScript is an Object Oriented Programming OOP language A programming In the following example, the constructor methods are Object, Array, and Date



[PDF] Does JavaScript Software Embrace Classes? - Alexandre Bergel

For example, JavaScript is now the most popular language at GitHub, considering like in Modula-2), and class-based object-oriented programming ( e g , a system is number of classes is pdf js (144 classes), followed by ace ( 133 classes) 



[PDF] Functional Programming in JavaScript

Functional and Object-oriented Programming in JavaScript Did you know that Packt offers eBook versions of every book published, with PDF and example, we provide it as an anonymous function that instantiates the objects and calls the



[PDF] JavaScript - ICS, UCI

Examples □ Functions, Objects, Linkages, Inheritance □ Web Development □ Comparison with It is an interpreted object-oriented programming language

[PDF] object oriented programming paradigm

[PDF] object oriented programming with abap objects

[PDF] object oriented programming with abap objects pdf

[PDF] objective c o'reilly pdf

[PDF] objective of education for all

[PDF] objective of water pollution project pdf

[PDF] objective proficiency 2nd edition pdf download

[PDF] objective proficiency student's book pdf download

[PDF] objective proficiency student's book pdf free download

[PDF] objective proficiency teacher's book pdf download

[PDF] objective proficiency teacher's book pdf free download

[PDF] objective proficiency teacher's book pdf

[PDF] objective c global function

[PDF] objective c static method

[PDF] objectives for christmas lesson plans

Functional and Object-Oriented Javascript

or

The Javascript Marty Doesn't Want You to Know

aka

How to make your

Javascript less

like this:And more like this: function init() { var items = document.getQuerySelectorAll(".thing"); for(var i = 0; i < items.length; i++) { function clickthing() { this.innerHTML = "clicked"; this.style.color = "red"; window.onload = init;$(function() { $(".thing").click(function() { What? Functional Javascript with anonymous methods and methods-as-variables. Objective Javascript with both Java-like and JSON notation.

Javascripts objects-as-hashes.

Why?

Most JS code out there won't look like Java code

(Will use at least some of the things listed above)

The big payoff: jQuery

(But jQuery won't make much sense without this!)

Functional JavascriptFunctions are variables too!

function test() { console.log("test!"); var test = function() { console.log("test!"); }is the same* as...

*actually there are a few minor differences involving the order the code is loaded, but don't mind meFunctions are variables too!

And what can we do with variables?

Functional Javascript

var test = function() { console.log("test"); function caller(fn) { fn(); caller(test);

What should this bit of code do?

Then - how do we get this work if test has a parameter?What should this bit of code do?

Functional Javascript

function isEven(x) { return x%2==0; Given this, write a method removeEvens(a) that takes an array (of numbers) and returns a copy of the passed in array, removing all the even elements. Then, generalize it to odds, or every third, etc.A more realistic example: function removeEvens(a) { var newa = []; return newa;

Functional JavascriptA more realistic example:

function removef(a,f) { var newa = []; for(var i = 0; i < a.length; i++) { if (!f(a[i])) { newa.push(a[i]); return newa; function isEven(i) { return i % 2 == 0; removef([1,2,3,4,5],isEven); //removes all evens What we just implemented here is filter, a basic operation in functional programming. (Javascript arrays have this by default, so we'll use that instead)

Functional JavascriptArray.filter

[1,2,3,4,5,6].filter(function(e){ return e%2==0}); //returns array of all evens in the array [-1,2,-3,4,-5,6].filter(function(e){ return e > 0}); //returns array of all the positives in the array ["abc","defg","hi"].filter(function(e){ return e.length == 2 //returns array of all strings of length 2

A few more to try:

Array of strings, remove all empty strings

Array of numbers, remove all contained in another array.

Functional JavascriptArray.map

[1,2,3,4].map(function(i){ return i+1; }) //returns [2,3,4,5] ["abc","bbbc","d"].map(function(i){ return i.length; }) //returns [3,4,1]Another common functional operator is called map. Think of this one as mapping between one array to another, given a mapping function.

A few more to try:

Array of numbers, map to their char (String.fromCharCode(i)) Array of strings, filter out the empty's and map to first letter

Functional JavascriptArray.reduce

a.reduce(function(prev,cur){ return running_sum; }, initial_value);Another common functional operator is called reduce. Think of it as combining all the elements of an array into one item, given an accumulator function. The syntax is different: [1,2,3,4,5].reduce(function(prev,cur) { return prev+cur; }, 0); //returns sum of all elements in the array ["abc","bbbc","d"].reduce(function(prev,cur) { return prev+cur; //what do you think this does?

Functional JavascriptArray.forEach

This one is pretty self explanatory.

(It's the foreach loop in javascript!)

It doesn't return anything.

[1,2,3].forEach(function(i) { console.log(i);

Note about all these functions (except foreach):

They don't modify the array. Instead, they return a new array.

Why Functional Javascript?

It's shorter.

Functions have closure, loops don't.

(You can use this to write shorter and better* code) The for loop and the array.forEach will behave differently for this html page.*opinion

Object-Oriented Javascript

How do you make an object in java?

Object o = new Object();

How do you make an object in javascript?

The same way!

var o = new Object();

Object-Oriented Javascript

Did you know you could do this?

var o = new Object(); o.lolwut = 5; console.log(o.lolwut);

What do you think this'll print?

Lesson:

Javascript objects can hold anything you store in them.

Object-Oriented Javascript

Did you know you could do this?

var o = new Object(); o["lolwut"] = 5; console.log(o.lolwut);

What do you think this'll print?

Lesson:

o["name"] and o.name notation are almost* equivalent *The almost comes from o["string with space"], can't do this the other way.

Object-Oriented Javascript

Another notation

var o = {lol:5, lolwut:"lolwut", lel:[1,2,3]};

Is equivalent to...

var o = new Object(); o.lol = 5; o["lolwut"] = "lolwut"; o.lel = [1,2,3];

The former is commonly called "JSON" notation.

Object-Oriented Javascript

Methods and Fields

This is object-oriented programming, so where are my methods and fields? var o = new Object(); o.lolwut = 5; o.test = function() { console.log("test");

And now you can go...

o.lol();

But how do we access fields?

Object-Oriented Javascript

Methods and Fields

This is object-oriented programming, so where are my methods and fields? var o = new Object(); o.lolwut = 5; o.test = function() { console.log(this.lolwut);

And now you can go...

o.lol();

Example

Count and display the occurrences of every word of a textfield. Hint: create a new object as your dictionary. Then, loop through all the words (split the text on space). Increment the dictionary at the word by 1.quotesdbs_dbs17.pdfusesText_23