Coder Social home page Coder Social logo

learningfs's People

Contributors

oldoc63 avatar

Watchers

 avatar

learningfs's Issues

Setters

  • Reassign values of existing properties within an object.
  • Like getter methods, there are similar advantages to using setter methods that include:
    • checking input,
    • performing actions on properties,
    • displaying a clear intention for how the object is supposed to be used.
  • even with a setter method, it is still possible to directly reassign properties.

Factory Functions

  • A factory function is a function that returns an object and can be reused to make multiple object instances.
  • Factory functions can also have parameters allowing us to customize the object that gets returned.

Block Scope

  • When a variable is defined inside a block, it is only accessible to the code within the curly braces {}.
  • Variables that are declared with block scope are known as local variables.

typeof

  • Useful to keep track of the data types of the variables.
const unknown3 = true; 
console.log(typeof unknown3); // Output: boolean

randomNumber

  • Math.random() returns a value between 0 (inclusive) and 1 (exclusive).
  • In order to make this set of numbers range from 0 (inclusive) to 8 (exclusive) we can multiple the returned value by 8.
  • Finally, to ensure we only have whole numbers from 0 to 7 we can round down using Math.floor().

Getters

  • Getters are methods that get and return the internal properties of an object.
  • We use the get keyword followed by a function.
  • We can access the calling object’s internal properties using this.
  • In general, getter methods do not need to be called with a set of parentheses.
  • Syntactically, it looks like we’re accessing a property.

Control Flow: Switch

  • Control Flow must:
    • Takes the condition: switch(randomNumber)
    • Assigns eightBall to a reply that a Magic Eight Ball would return.
switch (condition) {
  case 0:
    eightBall = 'prediction here';
    break;
  case 1:
    eightBall = 'another prediction here';
    break;  
  // additional cases...
}

Switch

  • We often find ourselves needing to check multiple values and handling each of them differently.
  • A switch statement provides an alternative syntax to else if that is easier to read and write.
let groceryItem = 'papaya';
 
switch (groceryItem) {
  case 'tomato':
    console.log('Tomatoes are $0.49');
    break;
  case 'lime':
    console.log('Limes are $1.49');
    break;
  case 'papaya':
    console.log('Papayas are $1.29');
    break;
  default:
    console.log('Invalid item');
    break;
}
 
// Prints 'Papayas are $1.29'

Getter and Setter

  • Create getter and setter methods for the appetizers, mains, and desserts properties.

let

  • The let keyword was introduced in ES6.
  • The let keyword signals that the variable can be reassigned a different value.
  • We can declare a variable without assigning the variable a value.
  • The variable will be automatically initialized with a value of undefined.
let price;
console.log(price); // Output: undefined
price = 350;
console.log(price); // Output: 350

Concatenation

  • The + operator concatenates two string values even if those values are being stored in variables.

var

  • Prior to the ES6, programmers could only use the var keyword to declare variables.
  • After the variable is declared, is printed to the console by referencing the variable name: console.log(var);

Tightly scope your variables is a good scoping practice

  • It will make your code more legible and understandable since the blocks will organize your code into discrete sections.
  • It’s easier to maintain your code, since your code will be modular.
  • It will save memory in your code because it will cease to exist after the block finishes running.
  • If a variable does not need to exist outside a block— it shouldn’t!

Translating text to whale language

  • Take a phrase like ‘turpentine and turtles’ and translate it into its “whale talk” equivalent: ‘UUEEIEEAUUEE’.
  • There are a few simple rules for translating text to whale language:
    • There are no consonants. Only vowels excluding “y”.
    • The u‘s and e‘s are extra long, so we must double them in our program.

Objects Literals

  • Objects can be assigned to variables just like any JavaScript type.
  • We use curly braces, {}, to designate an object literal:
let spaceship = {}; // spaceship is an empty object
  • We fill an object with unordered data.
  • This data is organized into key-value pairs.
  • A key is like a variable name that points to a location in memory that holds a value.
  • A key’s value can be of any data type in the language including functions or other objects.
  • We make a key-value pair by writing the key’s name, or identifier, followed by a colon and then the value.
  • We separate each key-value pair in an object literal with a comma (,).
  • Keys are strings.
  • When we have a key that does not have any special characters in it, JavaScript allows us to omit the quotation marks.

Meal Lover

  • You love trying out new restaurants and experimenting with different foods.
  • Use JavaScript to randomly create a three-course meal based on what is available on a menu.
  • Create an empty menu object.
  • Add _courses property to yor menu.
  • Create three properties inside the _courses object called appetizers, mains, and desserts.
  • Initialize them to an empty array.

Property Assignment

  • Objects are mutable meaning we can update them after we create them.
  • We can use either dot notation, ., or bracket notation,[], and the assignment operator,= to add new key-value pairs to an object or change an existing property.
  • If the property already exists on the object, whatever value it held before will be replaced with the newly assigned value.
  • If there was no property with that name, a new property will be added to the object.
    -Although we can’t reassign an object declared with const, we can still mutate it:
const spaceship = {type: 'shuttle'};
spaceship = {type: 'alien'}; // TypeError: Assignment to constant variable.
spaceship.type = 'alien'; // Changes the value of the type property
spaceship.speed = 'Mach 5'; // Creates a new key of 'speed' with a value of 'Mach 5'

Race numbers are assigned randomly.

  • Math.random() returns a value between 0 (inclusive) and 1 (exclusive).

  • In order to make this set of numbers range from 0 (inclusive) to 1000 (exclusive) we can multiply the returned value by 1000.

  • Finally, to ensure we only have whole numbers from 0 to 999 we can round down using Math.floor().

Methods

  • When the data stored on an object is a function we call that a method.
  • A property is what an object has, while a method is what an object does.
  • We can include methods in our object literals by creating ordinary, comma-separated key-value pairs.
  • The key serves as our method’s name, while the value is an anonymous function expression: invade: function () { ...
  • With the new method syntax introduced in ES6 we can omit the colon and the function keyword:
const alienShip = {
  invade () { 
    console.log('Hello! We have come to dominate your planet. Instead of Earth, it shall be called New Xaculon.')
  }
};
  • Object methods are invoked by appending the object’s name with the dot operator followed by the method name and parentheses: alienShip.invade();

Scope and Block

  • Scope defines where variables can be accessed or referenced.
  • A block is the code found inside a set of curly braces {}.
    • A function body or an if statement:
    const logSkyColor = () => {
    let color = 'blue'; 
    console.log(color); // blue 
    }
    

Global Scope

  • In global scope, variables are declared outside of blocks.
  • These variables are called global variables.

Destructuring

  • ES6 introduced some new shortcuts for assigning properties to variables known as destructuring.

Magic8Ball: Ternary

  • Create a ternary expression that decides what to do if the user enters a name or not.

this keyword

  • Objects are collections of related data and functionality.
  • We store that functionality in methods on our objects.

Nested Objects

  • Objects are often nested.
  • We can chain operators to access nested properties.

Increment and Decrement

  • The increment operator:
    • Increase the value of the variable by 1.
  • The decrement operator:
    • Decrease the value of the variable by 1.
let a = 10;
a++;
console.log(a); // Output: 11

Destructured assignment

  • We often want to extract key-value pairs from objects and save them as variables.
  • In destructured assignment we create a variable with the name of an object’s key that is wrapped in curly braces { } and assign to it the object.
  • We can even use destructured assignment to grab nested properties of an object

User Question

  • Create a variable userQuestion and print it with your name using console.log.

Pass by reference

  • This means when we pass a variable assigned to an object into a function as an argument, the computer interprets the parameter name as pointing to the space in memory holding that object.
  • As a result, functions which change object properties actually mutate the object permanently (even when the object is assigned to a const variable).

const

  • Introduced in ES6, and is short for the word constant.
  • const variable cannot be reassigned. If you try to reassign a const variable, you’ll get a TypeError.
  • Constant variables must be assigned a value when declared. If not SyntaxError.
  • Think about whether you’ll need to reassign the variable later on, to decide between let and constant.

Privacy

  • Getters can return the value of internal properties and setters can safely reassign property values.
  • Certain languages have privacy built-in for objects, but JavaScript does not have this feature.
  • One common convention is to place an underscore _ before the name of a property to mean that the property should not be altered.

Interpolation

  • In the ES6 version of JavaScript, we can insert, or interpolate, variables into strings using template literals.
const myPet = 'armadillo';
console.log(`I own a pet ${myPet}.`);
// Output: I own a pet armadillo.

Ternary

  • Used to simplify an if...else statement.
  • The condition is provided before the ?
  • Two expressions follow the ? and are separated by a colon :
  • If the condition evaluates to true, the first expression executes.
  • If the condition evaluates to false, the second expression executes.

Start the game and log the results

  • Create a function named playGame
  • Inside the playGame() function, call the determineWinner() function
  • Call playGame() on the last line of your program

Scope Pollution

  • Scope pollution is when we have too many global variables that exist in the global namespace, or when we reuse variables across different scopes.

Accesing Properties

  • There are two ways we can access an object’s property.
  • Dot Notation (.): hello.length;
  • With property dot notation, we write the object’s name, followed by the dot operator and then the property name (key).
let spaceship = {
  homePlanet: 'Earth',
  color: 'silver'
};
spaceship.homePlanet; // Returns 'Earth',
spaceship.color; // Returns 'silver',
  • If we try to access a property that does not exist on that object, undefined will be returned.

Arrow functions and this

  • The use of this becomes a bit more complicated when we start using arrow functions for methods.
const goat = {
  dietType: 'herbivore',
  makeSound() {
    console.log('baaa');
  },
  diet: () => {
    console.log(this.dietType);
  }
};
 
goat.diet(); // Prints undefined
  • the value of this in this case is the global object, without a property dietType, no the calling object.
  • Avoid using arrow functions when using this in a method!

Looping Through Objects

  • We learned how to iterate through arrays using their numerical indexing, but the key-value pairs in objects aren’t ordered.
  • In JS the alternative is to use a for ... in loop.
  • for ... in will execute a given block of code for each property in an object.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.