Skip to content

Instantly share code, notes, and snippets.

const data = { username: 'example' };
fetch('https://example.com/profile', {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((response) => response.json())
@SanjeQi
SanjeQi / Classes_subClasses.js
Last active August 4, 2020 16:13
Classes and subClasses ES6
////////////////////////////////////////////////////Classes and subClasses
//ES5
var Person5 = function (name, yearOfBirth, job) {
this.name = name;
this.yearOfBirth = yearOfBirth;
this.job = job;
};
Person5.prototype.calcAge = function () {
Get the average pixels
function getAveragePixels(cssObj) {
return Object
.values(cssObj)
.filter(el => el.includes('px'))
.map(el => parseInt(el))
.reduce((acc, nr, index, array) => acc = (acc + nr) / array.length)
}
//npm install --dev eslint prettier eslint-config-airbnb@^15.0.1 eslint-config-prettier eslint-plugin-prettier eslint-plugin-react eslint-plugin-import eslint-plugin-jsx-a11y@^5.1.1
{
"extends": ["airbnb", "prettier", "prettier/react"],
"plugins": ["prettier"],
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 2016,
"sourceType": "module",
"ecmaFeatures": {
@SanjeQi
SanjeQi / js_REST.js
Last active December 17, 2018 10:53
GET /newsletters # Show all newsletters
POST /newsletters # Create a new newsletter
GET /newsletters/new # Render the form for creating a new newsletter
GET /newsletters/:id/edit # Render the form for editing a newsletter
GET /newsletters/:id # Show a specific newsletter
PATCH /newsletters/:id # Update a newsletter
DELETE /newsletters/:id # Delete a newsletter
-----------------------------------------------------------------------------
Nested Routes
#We often need to access resources as they relate to parent resources, and our RESTful
document.all;
// returns all the nodes inside the document object
document.contentType;
// returns the type of content contained. Most web pages should return "text/html"
document.URL;
// returns the URL of the document object
----------------------------------------------
@SanjeQi
SanjeQi / js_arrays.js
Last active August 8, 2022 16:10
Methods on arrays info. Important !!!
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
// => undefined
alphabet.length;
// => 26
alphabet[alphabet.length - 1];
// => "z"
Number.isInteger()
Number.isInteger(42);
//=> true
Number.isInteger(0.42);
//=> false
------------------------------
Number.isFinite()
Number.isFinite(9001);
class SongsController < ApplicationController
before_action :set_song, only: [:show, :edit, :update, :destroy]
def index
@songs = Song.all
end
def new
class AuthorsController < ApplicationController
def create
@author = Author.new(author_params)
if @author.valid?
@author.save
redirect_to author_path(@author)
else
render :new
end