Javascript Introduction

HTML to define the content of web pages,
CSS to specify the layout of web pages and finally,
JavaScript to program the behavior of web pages

Scripts

JavaScript must be written inside the <script> tag in HTML. Scripts can be placed in either the <head>, <body> or both if need be. Scripts can also be placed in an external file and it’s more practical to use this method when many HTML documents make use of the same JavaScript.
JavaScript files have the extension .js

JavaScript Methods

getElementById();

JavaScript Output

  • innerHTML
  • document.write()
  • window.alert()
  • console.log

5 differences between HTML and HTML5

Multimedia Support

HTML doesn’t support for video and audio in language
HTML5 has them integrated into it

Geographical Support

It is harder in HTML to track user’s location when they are using mobile devices
HTML5 uses javascript Geolocation API which is used to identify the location of any user that uses the website

Error handling

HTML cannot handle inaccurate syntax and any other errors
HTML5 is capable of handling inaccurate syntax and other errors

Graphics Support

In HTML, Vector Graphics support is possible with the help of other tools
In HTML5, Vector Graphics is supported by default and it has canvas and SVG inbuilt

Browser Compatibility

HTML is compatible with all the browsers as it has been there for a really long time and browsers did enough modifications to support all the features of the HTML.
HTML5 is relatively new and not all browsers support all the new tags and features of HTML5

References

https://www.educba.com/html-vs-html5/

GitHub Help

GitHub for Windows
https://windows.github.com

GitHub for Mac
https://mac.github.com

git rm[file]
Deletes the file from the working directory and stages the deletion

git stash
Temporarily stores all modified tracked files

git init [project-name]
Creates a new local repository

git clone [url]
Downloads a project and its entire version history

git status
lists all new or modified files to be committed

git branch
Lists all local branches in the current repository

git merge
Combines the specified branch’s history into the current branch

References

Github Cheat Sheet

CSS: Favorites

The CSS Universal Selector (*) selects all the HTML elements on the page

The Grouping Selector is performed by using commas between elements with the same code.

Backgrounds

  • background-color: colorName
  • background-image: url(“location”)
  • background-repeat: repeat-x/y – make it so background image is only represented horizontally or vertically.
  • background-repeat: no repeat – make it so background image is only showed once

outline-style: solid/double/outset
outline-color: red/blue…….

color: colorName – Changes the color text

border: [#]px: colorName – Places a border around the text you want

Image Sprites – if you want any tabs to have sprites, you can use image sprites

Specificity Hierarchy

Every selector has its place in the specificity hierarchy. There are four categories which define the specificity level of a selector:

  • Inline styles – An inline style is attached directly to the element to be styled. Example: <h1 style=”color: #ffffff;”>.
  • IDs – An ID is a unique identifier for the page elements, such as #navbar.
  • Classes, attributes and pseudo-classes – This category includes .classes, [attributes] and pseudo-classes such as :hover, :focus etc.
  • Elements and pseudo-elements – This category includes element names and pseudo-elements, such as h1, div, :before and :after.

Design Pattern: Singleton

What is a singleton?

It is design pattern that uses only one object or instance of an object at a time.

Steps to create a Singleton pattern

  • Use getInstance() method as it is used universally for a singleton class

Example

Let’s take an example of Blackjack. In the game of blackjack, the dealer deals two cards to each player in the game. In the instance created would be the whole deck of cards that the dealer needs to deal. In this case, you would only need one instance of that class since you can only use one deck of cards. The game wouldn’t be fair if the dealer deals from different deck of cards for each player. So, once the dealer deals a card, that card will be removed from the list and the list contains the remaining cards to be dealt. Thats why a singleton class makes sense since you have to use the cards from only that instance. Check out the GitHub link for the code on this.

References

Design Pattern: Facade Pattern

The Facade pattern does all the actions that need to be done by a class in the background. It will give it a false sense of not doing a lot of work but in reality all the work is done by other classes.

Steps to create a Facade Pattern

Create a class that will need to perform a certain task.

Create another class that has the implementations of these methods.

Call these methods from the first class

Explanation

As you can see from the steps above, the first class just contains the methods that needs to be performed for the program to run. All these methods are created and implemented in another class. The first class will not see how these methods are performed and all the implementations are encapsulated within another class. In conclusion, you can see that all actions are performed in the background.

References

Code

Design Pattern: Observer Pattern

What is it?

It ensures that when on object changes state, all its dependencies are notified and updated automatically. It defines a one-to-many dependency.

The key objects in this pattern are the subject and the observer. When there is a change in state with the subject, all the observers are notified. In response to the change, the observer will synchronize with the subject’s state.

When to use the observer pattern?

  • When change to one object requires change to another and there is no knowledge of how many objects need to be changed.
  • When you don’t want any tightly coupled objects.

Steps to create an Observer pattern

  • Create a Subject and Observer patterns. The subject will register, unregister or notify the observers of any changes occurring during runtime. The observer will have the method/methods of variables that need to be updated or changed.
  • Create a class that implements the subject.
  • Create an array list in this class containing all the observers.
  • Create the methods for registering and unregistering the observers as well as notifying the observers of any change occurring.
  • Create a class that implements the observer.
  • In this class, make updates to the variables that need to be changed.
  • Use a main class where you can make changes to the variables dynamically. These values will be passed to the method implementing the subject class where the values will be set. When these values are set, notify the observers of this change so the change could take place.

References

Design Pattern: Strategy Pattern

Description

It defines a family of algorithms, encapsulates each one of them and makes them interchangeable. This means that reusability increases while preventing the code from breaking.

When should you use Strategy?

  • When many related classes different only in one behavior. This way, you don’t constantly have to implement/write code for every class that uses that specific funtion
  • It enables an algorithm’s behavior to be selected at runtime.

Example

Let’s take a look at an example from geeksforgeeks.
They use an example of the game “Street Fighter” where there are 4 types of fighters. They can all use the moves jump(), kick(), roll(), duck() but each of their implementation is different. In this case, you can use an interface to make sure the character has to perform all these moves. What if only a few of the characters can perform break().

The solution to this is that we have to either put it in the interface and implement it in everyclass or write a block of code for it for every character that uses it. Too much work don’t you think?

You can always use the strategy pattern to solve this type of problem.

Steps to create a Strategy Pattern

  • Create an interface containing the method/methods that need to be implemented by certain classes
  • Create other classes that implements the interface and need to be dynamically called at runtime. These are the classes that contain the different methods of a certain strategy
  • Make an instance of the interface
  • Using composition, call the methods depending on the situation of the use

Conclusion

You want to use the strategy pattern when a class has only one behavior that is similar to other behaviors in a list and you want to dynamically choose the behavior. Basically, it goes around the overriding every single implementation of a method if the behavior is similar.

References

The video by Derek Banas explains the Strategy Design pattern extremely well and I suggest you check it out after reading the description

Design Pattern: Factory Pattern

How to create a factory pattern?

Define an interface for creating an object, but let subclasses decide which class to instantiate.

When to use factory pattern?

  • Use it when a class cannot predict which class of objects that it must create.
  • Use it when you want a subclass to specify the objects it must create
  • Use it when you want a class to delegate responsibility to a helper subclasses

Conclusion

You need to understand that classes are chosen at runtime

Conclusion

When a method returns one of many possible classes that share a common super class.

Design a site like this with WordPress.com
Get started