Published
Edited
Oct 20, 2018
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const Car = { jezdzij: () => 'Brum, brum' }
const stworzSamochod = {
fabryka: (type) => {
if (type === 'SUV') {
const suv = Object.assign({}, Car);
suv.jezdzij = () => 'Brum, brum, mam 5 drzwi'
return suv
}
}
}

const cherokee = stworzSamochod.fabryka('SUV');
return cherokee.jezdzij();
}
Insert cell
Insert cell
Insert cell
Insert cell
{
const obj1 = {name: 'Jan', age: 20}
const obj2 = {name: 'Kasia', age: 18}
const obj3 = {name: 'Kasia', age: 18}

Object.prototype.showName = function() {
return 'Nazywam się ' + this.name;
}

return [
obj1.showName(),
obj2.showName(),
obj3.showName(),
obj2,
obj3,
obj2 == obj3
]
}
Insert cell
Insert cell
{
function Hero(name, age) {
// sprawdzenie czy byl juz tworzony obiekt tej klasy
if (typeof Hero.instance === 'object') {
return Hero.instance;
}

// wypełnianie atrybutów nowego obiektu
this.name = name;
this.age = age;

// zapamiętanie pierwszego obiektu
Hero.instance = this;
return this;
}

var player1 = new Hero('John', 20);
var player2 = new Hero('Sam', 30);

return [player1, player2, player1 == player2];
}
Insert cell
Insert cell
Insert cell
{
class Sprzedaz {
constructor(price) {
this.price = price;
}
udekoruj(type) {
if (type === 'vat') {this.price = this.price + (this.price * 0.23) }
if (type === 'zl') {this.price = this.price + ' zł'}
}
pobierzCene() {
return this.price;
}
}
const sprzedaz = new Sprzedaz(100); // cena wynosi 100 zł
sprzedaz.udekoruj('vat'); // dodaj podatek VAT
sprzedaz.udekoruj('zl'); // sformatuj jako zł
return sprzedaz.pobierzCene(); // '123.00 zł‚
}
Insert cell
Insert cell
Insert cell
{
const zdarzenie = {
stop: function(e) {
e.preventDefault();
e.stopPropagation();
}
};
}
Insert cell
Insert cell
{
const zdarzenie = {
// ...
stop: function(e) {
// inne
if (typeof e.preventDefault === 'function' && typeof e.stopPropagation === 'function') {
e.preventDefault(); e.stopPropagation();
}
// IE
if (typeof e.returnValue === 'boolean' && typeof e.cancelBubble === 'boolean') {
e.returnValue = false; e.cancelBubble = true;
}
}
};
}
Insert cell
Insert cell
Insert cell
Insert cell
{
class Suma {
constructor(numbers) {
this.numbers = numbers;
this.currentElement = 0;
this.currentValue = 0;
}
next() {
if (this.numbers.length > this.currentElement +1) {
this.currentElement = this.currentElement +1;
this.currentValue = this.currentValue + this.numbers[this.currentElement];
return this.currentValue;
}
}
}
const suma = new Suma([0, 2, 4, 5]);
let element;
const result = [];
while (element = suma.next()) {
// wykonanie działań na elemencie...
result.push(element);
}
return result;
}
Insert cell
Insert cell
{
class Suma {
constructor(numbers) {
this.numbers = numbers;
this.currentElement = 0;
this.currentValue = 0;
}
next() {
this.currentElement = this.currentElement +1;
this.currentValue = this.currentValue + this.numbers[this.currentElement];

return this.currentValue;
}
hasNext() {
if (this.numbers.length > this.currentElement +1) {
return true;
}
return false;
}
}
const suma = new Suma([0, 2, 4, 5]);
const result = [];
while (suma.hasNext()) {
// wykonanie działań na następnym elemencie...
result.push(suma.next());
}
return result;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more