mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-01-11 14:37:56 -05:00
143 lines
2.1 KiB
JavaScript
143 lines
2.1 KiB
JavaScript
function eg1() {
|
|
let x = [];
|
|
x.push(x);
|
|
console.log(x); // [[]]
|
|
}
|
|
|
|
function eg1a() {
|
|
const x = [];
|
|
x.push(x); // error: x is const
|
|
}
|
|
|
|
function eg2() {
|
|
let x = [];
|
|
let y = [];
|
|
|
|
console.log(x === y); // true
|
|
|
|
x.push('hi');
|
|
|
|
console.log(x === y); // false
|
|
|
|
console.log({ x, y }); // { x: ['hi'], y: [] }
|
|
}
|
|
|
|
function eg3() {
|
|
class Foo {
|
|
x = 0;
|
|
|
|
inc() {
|
|
this.x++;
|
|
}
|
|
}
|
|
|
|
let f = new Foo();
|
|
f.inc();
|
|
console.log(f.x); // 1
|
|
|
|
let g = f;
|
|
g.inc();
|
|
console.log(g.x); // 2
|
|
console.log(f.x); // 1
|
|
}
|
|
|
|
function eg4() {
|
|
let x = 0;
|
|
|
|
function inc() {
|
|
x++;
|
|
return x;
|
|
}
|
|
|
|
console.log(inc()); // 1
|
|
console.log(inc()); // 1
|
|
console.log(inc()); // 1
|
|
|
|
console.log(x); // 0
|
|
x++;
|
|
console.log(x); // 1
|
|
x++;
|
|
console.log(x); // 2
|
|
|
|
console.log(inc()); // 1
|
|
}
|
|
|
|
function eg5() {
|
|
class BinaryTree {
|
|
constructor(x) {
|
|
this.value = x;
|
|
}
|
|
|
|
insert(x) {
|
|
if (this.value === undefined) {
|
|
this.value = x;
|
|
return;
|
|
}
|
|
|
|
if (x < this.value) {
|
|
if (this.left === undefined) {
|
|
this.left = new BinaryTree(x);
|
|
} else {
|
|
this.left.insert(x);
|
|
}
|
|
} else {
|
|
if (this.right === undefined) {
|
|
this.right = new BinaryTree(x);
|
|
} else {
|
|
this.right.insert(x);
|
|
}
|
|
}
|
|
}
|
|
|
|
contains(x) {
|
|
if (this.value === undefined) {
|
|
return false;
|
|
}
|
|
|
|
if (x < this.value) {
|
|
return this.left !== undefined && this.left.contains(x);
|
|
}
|
|
|
|
return this.right !== undefined && this.right.contains(x);
|
|
}
|
|
}
|
|
|
|
let tree = new BinaryTree();
|
|
|
|
tree.insert(5);
|
|
console.log(tree.contains(5)); // true
|
|
console.log(tree.contains(7)); // false
|
|
|
|
let tree2 = tree;
|
|
tree2.insert(7);
|
|
console.log(tree.contains(7)); // false
|
|
console.log(tree2.contains(7)); // true
|
|
}
|
|
|
|
function eg7() {
|
|
class Foo {
|
|
bar() {
|
|
this = 3;
|
|
}
|
|
}
|
|
|
|
let f = new Foo();
|
|
console.log(f); // Foo{}
|
|
|
|
f.bar();
|
|
|
|
console.log(f); // 3
|
|
}
|
|
|
|
function eg8() {
|
|
let x = {};
|
|
|
|
function addFoo() {
|
|
this.foo = 'bar';
|
|
}
|
|
|
|
addFoo.call(x);
|
|
|
|
console.log(x.foo); // 'bar'
|
|
}
|