var a = { n: 1 };
var b = a;
a.x = a = { n: 2 };
console.log(a);
console.log(b);
console.log(b.x);
a.n = 3;
console.log(b);
console.log(b.x);
console.log(a);
"use strict";
function sidEffecting(arr) {
arr[0] = arr[2];
}
function bar(a, b, c) {
c = 10;
sidEffecting(arguments);
return a + b + c;
}
console.log(bar(1, 1, 1));
var a = {};
var b = {
key: "a",
};
var c = {
key: "c",
};
a[b] = "123";
a[c] = "456";
console.log(a[b]);
var add = function (x1) {
var sum = x1;
var fun = function (x2) {
sum += x2;
return fun;
};
fun.toString = function () {
return sum;
};
return fun;
};
alert(add(1)(2)(3));
alert(add(2)(4)(6));
function add() {
var sum = 0;
for (i of arguments) {
sum += i;
}
return sum;
}
console.log(add(1, 2, 3));
console.log(add(1, 2, 3, 4, 5));
function add() {
var sum = 0;
for (i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
}
console.log(add(1, 2, 3));
console.log(add(1, 2, 3, 4, 5));
var arr = [1, 2, 3, 2, 1, 4];
var hash = {};
for (var i = 0; i < arr.length; i++) {
hash[arr[i]] = 1;
}
console.log(hash);
var res = [];
var i = 0;
for (res[i++] in hash);
console.log(res);
var arr = [];
for (var i = 0; i < 100000; i++) {
arr.push(parseInt(Math.random() * 10000));
}
function unique1(arr) {
var obj = {};
for (var n of arr) {
obj[n] = 1;
}
var newArr = [];
for (var key in obj) {
newArr.push(parseInt(key));
}
return newArr;
}
console.time("unique1");
var newArr = unique1(arr);
console.timeEnd("unique1");
console.log(newArr);
function unique2(arr) {
return [...new Set(arr)];
}
console.time("unique2");
var newArr = unique2(arr);
console.timeEnd("unique2");
console.log(newArr);
var arr = [1, 2, 3, "a", 4, "b"];
for (var i = arr.length - 1; i >= 0; i--) {
if (typeof arr[i] === "number") {
arr[i]++;
} else {
arr.splice(i, 1);
}
}
console.log(arr);
var arr1 = [1, 3, 7, 9, 12, 37, 45, 88];
var arr2 = [2, 4, 9, 13, 45, 88, 92];
for (var i = 0, j = 0, result = []; i < arr1.length && j < arr2.length; ) {
if (arr1[i] < arr2[j]) {
i++;
} else if (arr1[i] > arr2[j]) {
j++;
} else {
result.push(arr1[i]);
i++;
j++;
}
}
console.log(result);
var arr = [1, 2, 4, 6, 7, 11, 12, 15, 17];
for (var i = 0, j = arr.length - 1; i < j; ) {
if (arr[i] + arr[j] > 19) {
j--;
} else if (arr[i] + arr[j] < 19) {
i++;
} else {
console.log(`${arr[i]}+${arr[j]}`);
i++;
j--;
}
}
var age = 100;
function test() {
this.age = 50;
return function () {
return this.age;
};
}
var m = new test();
alert(m());
var n = test();
alert(n());
var name = "window";
var p = {
name: "Perter",
getName: function () {
var self = this;
return function () {
return self.name;
};
},
};
var getName = p.getName();
console.log(getName);
var _name = getName();
console.log(_name);
var a = 20;
function fun() {
this.a = 50;
return function () {
return this.a;
};
}
var m = new fun();
console.log(m());
var n = fun();
console.log(n());
<button>click me!</button>
<button>click me!</button>
<button>click me!</button>
<button>click me!</button>
<button>click me!</button>
var btns = document.getElementsByTagName("button");
console.log(btns);
for (var i = 0; i < btns.length; i++) {
(function (i) {
btns[i].onclick = function () {
alert(i);
};
})(i);
}
console.log("循环结束");
console.log(`i=${i}`);
console.log("程序结束");
for (var i = 0; i < 4; i++) {
setTimeout(function () {
console.log(i);
}, 50);
}
console.log("循环结束");
console.log("i=" + i);
function liang(next) {
console.log(`亮亮起跑....`);
setTimeout(function () {
console.log(`亮亮到达终点!`);
next();
}, Math.random() * 2 * 1000 + 1000);
}
function ran() {
console.log(`然然起跑....`);
setTimeout(function () {
console.log(`然然到达终点!`);
}, Math.random() * 2 * 1000 + 1000);
}
liang(
function () {
ran();
}
);
var str = "helloworld";
var arr = [];
for (var i = 0; i < str.length; i++) {
var char = str[i];
if (arr[char] === undefined) {
arr[char] = 1;
} else {
arr[char]++;
}
}
console.log(arr);
var maxchar,
count = 0;
for (var key in arr) {
if (arr[key] > count) {
maxchar = key;
count = arr[key];
}
}
console.log(`出现次数最多的字母是:${maxchar},出现了${count}次 `);
function clone(oldobj) {
var newobj = {};
for (var key in oldobj) {
newobj[key] = oldobj[key];
}
return newobj;
}
var lilei = {
sname: "lilei",
sage: 11,
};
var lilei2 = clone(lilei);
console.log(lilei);
console.log(lilei2);
console.log(lilei == lilei2);
function parent() {
var total = 1000;
return function (money) {
total -= money;
console.log(`花了${money},还剩${total}`);
};
}
total = 0;
var pay = parent();
pay(100);
pay(100);
pay(100);
function fun() {
var n = 999;
nAdd = function () {
n++;
};
return function () {
console.log(n);
};
}
var getN = fun();
getN();
nAdd();
getN();
function Emp(eid, ename, salary, eage) {
this.eid = eid;
this.ename = ename;
this.salary = salary;
this.eage = eage;
}
var eric = new Emp(1001, "埃里克", 1200, 25);
eric.eid = -2;
delete eric.ename;
for (var key in eric) {
console.log(`${key}:${eric[key]}`);
}
console.log(eric);
function deepClone(oldObj) {
if (oldObj == null) {
return null;
}
if (typeof oldObj !== "object") {
return oldObj;
}
var newObj = Array.isArray(oldObj) ? [] : {};
for (var key in oldObj) {
newObj[key] = deepClone(oldObj[key]);
}
return newObj;
}
var a1 = 10;
var arr1 = [1, 2, 3];
var lilei1 = {
sname: "Li Lei",
sage: 11,
address: {
city: "深圳",
street: "华强北",
phone: 1383838438,
email: "lilei@163.com",
},
};
var a2 = deepClone(a1);
console.log(a2);
var arr2 = deepClone(arr1);
console.log(arr2);
console.log(arr1 == arr2);
var lilei2 = deepClone(lilei1);
console.log(lilei2);
console.log(lilei1 == lilei2);
console.log(lilei1.address == lilei2.address);
var num = new Array();
for (var i = 0; i < 4; i++) {
num[i] = f1(i);
}
function f1(n) {
function f2() {
alert(n);
}
return f2;
}
num[2]();
num[1]();
num[0]();
num[3]();
function Foo() {
Foo.a = function () {
console.log(1);
};
this.a = function () {
console.log(2);
};
}
Foo.prototype.a = function () {
console.log(3);
};
Foo.a = function () {
console.log(4);
};
Foo.a();
let obj = new Foo();
obj.a();
Foo.a();
var x = 0;
var foo = {
x: 1,
bar: function () {
console.log(this.x);
var that = this;
return function () {
console.log(this.x);
console.log(that.x);
};
},
};
foo.bar();
foo.bar()();
function fun(n, o) {
console.log(o);
return {
fun: function (m) {
return fun(m, n);
},
};
}
var a = fun(0);
a.fun(1);
a.fun(2);
a.fun(3);
var b = fun(0)
.fun(1)
.fun(2)
.fun(3);
var c = fun(0)
.fun(1);
c.fun(2);
c.fun(3);
function A() {}
function B() {
return new A();
}
A.prototype = new A();
B.prototype = new B();
var a = new A();
var b = new B();
console.log(a.__proto__ == b.__proto__);
function foo() {
getName = function () {
console.log(1);
};
return this;
}
foo.getName = function () {
console.log(2);
};
foo.prototype.getName = function () {
console.log(3);
};
var getName = function () {
console.log(4);
};
function getName() {
console.log(5);
}
foo.getName();
getName();
foo().getName();
getName();
new foo.getName();
new foo().getName();
new new foo().getName();
var getNum = (function () {
var i = 0;
return function () {
i++;
console.log(i);
};
})();
getNum();
getNum();
i = 0;
getNum();
getNum();
var n = 10,
str = "hello",
b = true,
nu = null,
un;
var f = function () {};
var obj1 = {},
obj2 = [1, 2, 3],
obj3 = new Date();
console.log(
typeof n,
typeof str,
typeof b,
typeof nu,
typeof un,
typeof f,
typeof obj1,
typeof obj2,
typeof obj3
);
console.log(
obj1.__proto__ == Array.prototype,
obj2.__proto__ == Array.prototype,
obj3.__proto__ == Array.prototype
);
console.log(
Object.getPrototypeOf(obj1) == Array.prototype,
Object.getPrototypeOf(obj2) == Array.prototype,
Object.getPrototypeOf(obj3) == Array.prototype
);
console.log(
Array.prototype.isPrototypeOf(obj1),
Array.prototype.isPrototypeOf(obj2),
Array.prototype.isPrototypeOf(obj3)
);
console.log(
obj1.constructor == Array,
obj2.constructor == Array,
obj3.constructor == Array
);
console.log(
obj1 instanceof Array,
obj2 instanceof Array,
obj3 instanceof Array
);
console.log(Object.prototype.toString.call(obj1));
console.log(Object.prototype.toString.call(obj2));
console.log(Object.prototype.toString.call(obj3));
for (let i = 0; i < 5; i++) {
setTimeout(function () {
console.log(i);
});
}
var number = 2;
var obj = {
number: 4,
fn1: (function () {
this.number *= 2;
number *= 2;
var number = 3;
return function () {
this.number *= 2;
number *= 3;
console.log(number);
};
})(),
};
var fn1 = obj.fn1;
console.log(number);
fn1();
obj.fn1();
console.log(number);
console.log(obj.number);
function fun(o) {
o.name = "西西";
o = {};
o.name = "小丽";
}
var obj = { name: "小红", age: 11 };
fun(obj);
console.log(obj);
var a = 2;
var obj = {
a: 4,
fn1: function () {
this.a *= 2;
var a = 3;
return function () {
this.a *= 2;
a *= 3;
console.log(a);
};
},
};
var fn1 = obj.fn1;
console.log(a);
fn1();
obj.fn1();
console.log(a);
console.log(obj.a);
function setObj(p) {
p.name = "小红";
p = {};
p.name = "茜茜";
}
var p = {
name: "小明",
};
setObj(p);
console.log(p);
var search =
"?uname=dingding&upwd=123456&favs=swimming&favs=running&favs=basketball";
function search2obj(str) {
var obj = {};
str = str.slice(1);
var arr = str.split("&");
for (var s of arr) {
var [key, value] = s.split("=");
if (obj[key] === undefined) {
obj[key] = value;
} else {
obj[key] = [].concat(obj[key], value);
}
}
return obj;
}
var obj = search2obj(search);
console.log(obj);
this.b = "2";
function abc() {
let b = 1;
++b;
setTimeout(() => {
test("fun text 第一个定时器");
}, 3000);
setTimeout(test("text fun 第二个定时器"), 3000);
console.log(b);
function test(str) {
this.b++;
console.log(str);
console.log(this.b++);
}
}
abc();
function sidEffecting(arr) {
arr[0] = arr[0];
}
function bar(a, b, c) {
c = 10;
sidEffecting(arguments);
return a + b + c;
}
console.log(bar(1, 1, 1));
#container {
width: 200px;
height: 200px;
border-radius: 5px;
background-color: purple;
}
#container > div {
width: 40px;
height: 40px;
background-color: #fff;
margin-top: 8px;
margin-left: 8px;
float: left;
border-radius: 4px;
line-height: 40px;
text-align: center;
cursor: pointer;
}
<div id="container"></div>
<script>
(function(){
var arr=[
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]
];
var div=document.getElementById("container")
for(var r=0;r<4;r++){
for(var c=0;c<4;c++){
var cell=document.createElement("div");
cell.innerHTML=`(${r},${c})`;
div.appendChild(cell);
cell.onclick=(function(r,c){
return function(){
arr[r][c]++;
alert(`点击了${arr[r][c]}次`);
}
})(r,c);
}
}
})();
</script>
setTimeout(() => console.log("a"), 0);
var p = new Promise((resolve) => {
console.log("b");
setTimeout(() => console.log("f"), 0);
resolve();
});
p.then(() => console.log("c"));
p.then(() => console.log("d"));
console.log("e");
var data = {
money: 1000,
setMoney(money) {
this.money = money;
this.notifyAll();
},
observers: [],
notifyAll() {
this.observers.forEach(function (obj) {
obj.getMoney();
});
},
};
var obj1 = {
money: 0,
getMoney() {
console.log(`obj1得知data的money被改为${data.money},并重新获得data.money`);
this.money = data.money;
},
};
var obj2 = {
money: 0,
getMoney() {
console.log(`obj2得知data的money被改为${data.money},并重新获得data.money`);
this.money = data.money;
},
};
var obj3 = {
money: 0,
getMoney() {
console.log(`obj3得知data的money被改为${data.money},并重新获得data.money`);
this.money = data.money;
},
};
data.observers.push(obj1);
data.observers.push(obj2);
data.observers.push(obj3);
data.setMoney(900);
console.log(obj1.money, obj2.money, obj3.money);
data.setMoney(800);
console.log(obj1.money, obj2.money, obj3.money);
<html>
<head>
<meta charset="utf-8"/>
<title>打开新链接方式总结</title>
<script>
function open1(){
window.open("http://baidu.com","_self");
}
function open2(){
location.replace("http://baidu.com");
}
function open3(){
window.open("http://baidu.com","_blank");
}
function open4(){
window.open("http://baidu.com","tmooc");
}
</script>
</head>
<body>
<h3>1. 在当前窗口打开,可后退</h3>
<a href="http://baidu.com" target="_self">欢迎访问tmooc</a><br>
<button onclick="open1()">欢迎访问tmooc</button>
<h3>2. 在当前窗口打开,禁止后退</h3>
<button onclick="open2()">欢迎访问tmooc</button>
<h3>3. 在新窗口打开,可同时打开多个</h3>
<a href="http://baidu.com" target="_blank">欢迎访问tmooc</a><br>
<button onclick="open3()">欢迎访问tmooc</button>
<h3>4. 在新窗口打开,同时只能打开一个</h3>
<a href="http://baidu.com" target="tmooc">欢迎访问tmooc</a><br>
<button onclick="open4()">欢迎访问tmooc</button>
</body>
</html>