Skip to content

Instantly share code, notes, and snippets.

@andreleite
Last active April 11, 2021 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreleite/ea2572659e13503bf7b1a47552a78474 to your computer and use it in GitHub Desktop.
Save andreleite/ea2572659e13503bf7b1a47552a78474 to your computer and use it in GitHub Desktop.
ES6 - React
console.clear();
var string = 'opcon4';
function changeString(string) {
string = 'opcon1';
console.log('inside', string);
}
changeString(string);
console.log('outside', string);
console.clear();
var number = 22;
function changeNumber(number) {
number = number + 20;
console.log('inside', number);
}
changeNumber(number);
console.log('outside', number);
console.clear()
var array = [
'backbone',
'require',
'gulp'
];
function changeArray(array) {
array[0] = 'react';
console.log('inside', array);
}
changeArray(array);
console.log('outside', array);
console.clear();
var developer = {
name: 'Orlando',
surname: 'Bustos'
};
function changeObject(developer) {
developer.name = 'Don ' + developer.name;
console.log('inside', developer);
}
changeObject(developer);
console.log('outside', developer);
console.clear();
var x = 2;
function square(x) {
x = x * x;
return x;
}
var newX = square(x);
console.log(newX);
console.log(
'same number?',
x === newX
);
console.clear()
var opcons = [1, 2, 3];
function addOpcon(opcons) {
opcons.push(4);
return opcons;
}
var newOpcons = addOpcon(opcons);
console.log(newOpcons);
console.log(
'same array?',
opcons === newOpcons
);
console.clear();
var opcons = [1, 2, 3];
function addOpcon(opcons) {
var newOpcons = [];
var i;
for(i = 0; i < opcons.length; i++) {
newOpcons[i] = opcons[i];
}
newOpcons[i] = 4;
return newOpcons;
}
var newOpcons = addOpcon(opcons);
console.log(newOpcons);
console.log(
'same array?',
opcons === newOpcons
);
console.clear()
var developer = {
name: 'Orlando'
};
function changeObject(developer) {
developer.name = 'Don ' + developer.name;
return developer;
}
var newDeveloper = changeObject(developer);
console.log(newDeveloper);
console.log(
'same object?',
developer === newDeveloper
);
console.clear();
var developer = {
name: 'Orlando'
};
function changeObject(developer) {
var newDeveloper = {};
for (var key in developer) {
newDeveloper[key] = 'Don ' + developer[key];
}
return newDeveloper;
}
var newDeveloper = changeObject(developer);
console.log(newDeveloper)
console.log(
'same object?',
developer === newDeveloper
);
console.clear()
let developer = {
name: 'Orlando'
}
const changeObject = (developer) => {
return Object.assign(
{},
developer,
{name: 'Don ' + developer.name}
)
}
var newDeveloper = changeObject(developer)
console.log(newDeveloper)
console.log(
'same object?',
developer === newDeveloper
)
console.clear()
let opcons = [1, 2, 3]
let addOpcon = (opcons) => {
return opcons.concat([4])
}
var newOpcons = addOpcon(opcons)
console.log(newOpcons)
console.log(
'same array?',
opcons === newOpcons
)
console.clear()
let array = [1, 2, 3]
console.log([0, ...array, 4])
const numbers = (num1, num2, num3) => {
console.log(num1, num2, num3)
}
numbers(...array)
console.clear()
let developer = {
name: 'Orlando',
surname: 'Bustos'
}
let developerWithKingOf = {kingOf: 'Sabre', ...developer}
console.log(developerWithKingOf)
console.clear()
const variableParams = (name, ...developer) => {
console.log(name, developer)
}
variableParams('Orlando', 'Bustos', 'Don')
console.clear()
let defaultValues = (opcon) => {
console.log(opcon)
}
defaultValues(4)
// Test without parameters and then create default parameter
console.clear()
let developer = {
name: 'Bladimir',
surname: 'Yanez',
kingOf: 'Sabre'
}
let { surname } = developer
console.log(surname)
import { component } from 'React'
class vuelos extends component {
}
// Extract name too
<script src="https://wzrd.in/standalone/expect@latest"></script>
<script src="https://wzrd.in/standalone/deep-freeze@latest"></script>
console.clear()
const toogleTodo = (todo) => {
//todo.completed = !todo.completed
return Object.assign(
{},
todo,
{completed: !todo.completed}
)
return todo
}
const testToogleTodo = () => {
const todoBefore = {
id: 0,
text: 'Make a favor to Blad to join the mafia',
completed: false
}
const todoAfter = {
id: 0,
text: 'Make a favor to Blad to join the mafia',
completed: true
}
deepFreeze(todoBefore)
expect(
toogleTodo(todoBefore)
).toEqual(todoAfter)
}
testToogleTodo()
console.log('All tests clear')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment