javascript's People
javascript's Issues
Styling DOM elements
Creating, Inserting and Removing Elements
What is DOM?
- Javascript is a hosted language.
- The browser engine provides the environment for the Javascript engine to operate.
- The HTML content downloaded at the browser end is loaded and parsed by the browser.
- It parses the HTML file top to bottom. If it sees an HTML file, it renders and if it sees a javascript file, it executes it.
- As a result of this, the pixels are displayed on the browser.
The document is a property of the windows object. The document object is the entry to all the operations which can be performed on a DOM.
More Info on DOM and Browser APIs
Getting element box dimensions:
> $0.getBoundingClientRect()
DOMRect {x: 144.61459350585938, y: 70, width: 88.32292175292969, height: 26.666667938232422, top: 70, …}
bottom: 96.66666793823242
height: 26.666667938232422
left: 144.61459350585938
right: 232.93751525878906
top: 70
width: 88.32292175292969
x: 144.61459350585938
y: 70
__proto__: DOMRect
Objects and its properties
Arrays and Iterables
Maps and Sets
Async Javascript - Promises and Callbacks
Working with Http requests
Sending a GET request:
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts');
xhr.onload = function() {
console.log(xhr.response);
};
xhr.send();
JSON data and parsing data:
const listElement = document.querySelector('.posts');
const postTemplate = document.getElementById('single-post');
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts');
xhr.responseType = 'json';
xhr.onload = function() {
// const listOfPosts = JSON.parse(xhr.response);
const listOfPosts = xhr.response;
for (const post of listOfPosts) {
const postEl = document.importNode(postTemplate.content, true);
postEl.querySelector('h2').textContent = post.title.toUpperCase();
postEl.querySelector('p').textContent = post.body;
listElement.append(postEl);
}
};
xhr.send();
this keyword and getters/setters
The this keyword can lead to some headaches in JavaScript - this summary hopefully acts as a remedy.
this refers to different things, depending on where it's used and how (if used in a function) a function is called.
Generally, this refers to the "thing" which called a function (if used inside of a function). That can be the global context, an object, or some bound data/ object (e.g. when the browser binds this to the button that triggered a click event).
- this in Global Context (i.e. outside of any function)
function something() { ... }
console.log(this); // logs global object (window in browser) - ALWAYS (also in strict mode)!
- this in a Function (non-Arrow) - Called in the global context
function something() {
console.log(this);
}
something(); // logs global object (window in browser) in non-strict mode, undefined in strict mode
- this in an Arrow-Function - Called in the global context
const something = () => {
console.log(this);
}
something(); // logs global object (window in browser) - ALWAYS (also in strict mode)!
- this in a Method (non-Arrow) - Called on an object
const person = {
name: 'Max',
greet: function() { // or use method shorthand: greet() { ... }
console.log(this.name);
}
};
person.greet(); // logs 'Max', "this" refers to the person object
- this in a Method (Arrow Function) - Called on an object
const person = {
name: 'Max',
greet: () => {
console.log(this.name);
}
};
person.greet(); // logs nothing (or some global name on window object), "this" refers to global (window) object, even in strict mode
this can refer to unexpected things if you call it on some other object, e.g.:
const person = {
name: 'Max',
greet() {
console.log(this.name);
}
};
const anotherPerson = { name: 'Manuel' }; // does NOT have a built-in greet method!
anotherPerson.sayHi = person.greet; // greet is NOT called here, it's just assigned to a new property/ method on the "anotherPerson" object
anotherPerson.sayHi(); // logs 'Manuel' because method is called on "anotherPerson" object => "this" refers to the "thing" which called it
If in doubt, a console.log(this); can always help you find out what this is referring to at the moment!
Live node lists vs Static node lists
Evaluating and Manipulating elements
Classes and Object-Oriented Programming
Advance Functions in Javascript
Working with Events
Click event
const button = document.querySelector('button');
const buttonClickHandler = () => {
alert('Button clicked!!');
}
// button.onclick = buttonClickHandler;
button.addEventListener('click', buttonClickHandler);
setTimeout(() => {
button.removeEventListener('click', buttonClickHandler);
}, 2000);
Numbers and Strings
Constructors functions and Prototypes
Querying Elements
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.