function fun(num){
num = 100;
}
let n = 10;
fun(n);
console.log(n);
//10
function fun(arr) {
arr.push(10);
};
let a = [1, 2, 3];
fun(a);
console.log(a);
//[ 1, 2, 3, 10 ]
let stu = {
name: 'xiaoming',
age: 10,
}
function copyObject(obj) {
let newObj = {}
for (let i in obj) {
newObj[i] = obj[i]
}
return newObj
}
let newStu = copyObject(stu)
stu.age = 20
console.log(stu)
console.log(newStu)
let stu = {
name:"xiaoming",
age:10,
bestFriend:{
name:"xiaohong",
age:10
}
}
function copyObject(obj){
let newObj = {};
for(let i in obj){
if(obj[i] instanceof Object){
newObj[i] = copyObject(obj[i]);
}else{
newObj[i] = obj[i];
}
}
return newObj;
}
let newStu = copyObject(stu);
stu.bestFriend.name = "xiaofen";
console.log(stu);
console.log(newStu);
new Promise(function (resolve, reject) {
var a = 0
var b = 1
if (b == 0) reject('0')
else resolve(a / b)
})
.then(function (value) {
console.log('a / b = ' + value)
})
.catch(function (err) {
console.log(err)
})
.finally(function () {
console.log('end')
})
// a / b = 0
// end
function User() {
this.login = function () {
console.log('登录成功')
}
}
function Admin() {}
Admin.prototype = new User()
var admin = new Admin()
admin.login()
//登录成功
function Person() {
this.id = '007'
}
function User() {
this.login = function () {
console.log('登录成功')
}
}
User.prototype = new Person()
function Admin() {}
Admin.prototype = new User()
var admin = new Admin()
console.log(admin.id)
Object.prototype.sayHi = function () {
console.log('hello word')
}
admin.sayHi()
//007
//hello word
new Promise(function (resolve, reject) {
console.log(111)
resolve(1122)
})
.then(function (value) {
console.log(value)
return 112233
})
.then(function (value) {
console.log(value)
throw 'an error'
})
.catch(function (err) {
console.log(err)
})
//111
//1122
//112233
//an error
function foo(x) {
console.log(arguments)
return x
}
foo(1, 2, 3, 4, 5)
;(function foo3(x) {
console.log(arguments)
return x
})(1, 2, 3, 4, 5)
//[Arguments] { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 }
//[Arguments] { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 }
let yy = (function () {
let a = 30
let b = 20
function add() {
return a + b
}
function acc() {
return a - b
}
return {
add,
acc,
}
})()
let result1 = yy.add()
let result2 = yy.acc()
console.log(result1)
console.log(result2)
//50
//10
var x = 1,
y = (z = 0)
function add(n) {
return (n = n + 1)
}
y = add(x)
function add(n) {
return (n = n + 3)
}
z = add(x)
console.log(x, y, z)
//1 4 4
function a() {
return new Promise((resolve) => {
setTimeout(() => {
resolve('a......')
}, 2000)
})
}
async function b() {
console.log('b.....')
const result = await a()
console.log(result)
}
b()
//b.....
//a......
var o = {
a: 2,
m: function () {
return this.a + 1
},
}
console.log(o.m())
var p = Object.create(o)
p.a = 4
console.log(p.m())
//3
//5
var obj1 = {
age: 10,
sex: "男",
car: ["宝马", "奔驰", "奥迪", "特斯拉"],
dog: {
name: "大黄",
age: 5,
color: "白色",
},
};
var obj2 = {};
function extend(a, b) {
for (var key in a) {
var item = a[key];
if (item instanceof Array) {
b[key] = [];
extend(item, b[key]);
} else if (item instanceof Object) {
b[key] = {};
extend(item, b[key]);
} else {
b[key] = item;
}
}
}
extend(obj1, obj2);
console.log(obj1);
console.log(obj2);
var obj1 = {
age: 10,
sex: "男",
car: ["奔驰", "宝马", "奥迪", "特使拉"],
};
var obj2 = {};
// obj2["age"]=obj1.age;
function extend(a, b) {
for (var key in a) {
b[key] = a[key];
}
}
extend(obj1, obj2);
console.log(obj2);
console.log(obj1);
<body>
<h2 class="title"></h2>
<button class="btn">改变文本</button>
<script>
let message="hello world";
const titleEl = document.getElementsByClassName("title")[0];
titleEl.innerHTML = message;
const btnEl = document.getElementsByClassName("btn")[0];
btnEl.addEventListener("click",e=>{
message = "hello React";
titleEl.innerHTML = message;
})
</script>
</body>
var a = 0
b = 0
function A(a){
A = function(b){
alert(a + b++)
}
alert(a++)
}
A(3) //3
A(2) //2
// A(3)
// A(2)
// 3 6
// A(6)
// A(5)
//6 12
var test = (function (i) {
return function () {
alert((i *= 2))
}
})(4)
test(5)
//8
test(2)
//8
var x = 2
var y = {
x: 3,
z: (function (x) {
this.x *= x
x += 2
return function (n) {
this.x *= n
x += 3
console.log(x)
}
})(x),
}
var m = y.z
m(4)
y.z(5)
console.log(x, y.x)
//7
//10
//16 15
var x = 0,
y = 1
function fn() {
x += 2
fn = function (y) {
console.log(y + --x)
}
console.log(x, y)
}
fn(3)
fn(4)
console.log(x, y)
//2 1
//5
//1 1
setTimeout(() => {
console.log(1)
}, 20)
console.log(2)
setTimeout(() => {
console.log(3)
}, 10)
console.log(4)
console.log('AA')
for (let i = 0; i < 9000000; i++) {}
console.timeEnd('AA')
console.log(5)
setTimeout(() => {
console.log(6)
}, 8)
console.log(7)
setTimeout(() => {
console.log(8)
}, 15)
console.log(9)
//2
//4
//AA
//5
//7
//9
//3
//6
//1
//8
async function async1() {
console.log('async1 start')
await async2()
console.log('async1 end')
}
async function async2() {
console.log('async2')
}
console.log('script start')
setTimeout(function () {
console.log('setTimeout')
}, 0)
async1()
new Promise(function (resolve) {
console.log('promise1')
resolve()
}).then(function () {
console.log('promise2')
})
console.log('script end')
//script start
//async1 start
//async2
//promise1
//script end
//async1 end
//promise2
//setTimeout
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()
//2
//4
//1
//1
//2
//3
//3
let a = {}
;(b = {
n: '1',
}),
(c = {
m: '2',
})
a[b] = '珠峰'
a[c] = '培训'
console.log(a[b])
//培训
let a = {}
b = Symbol('1')
c = Symbol('1')
a[b] = '珠峰'
a[c] = '培训'
console.log(a[b])
//珠峰
let a = {}
b = '0'
c = 0
a[b] = '珠峰'
a[c] = '培训'
console.log(a[b])
//培训
let Tea = new Promise((resolve, reject) => {
setTimeout(() => {
let flag = true
if (flag) {
resolve('奶茶~')
} else {
reject('获取奶茶失败')
}
}, 2000)
})
let HotPot = new Promise((resolve, reject) => {
setTimeout(() => {
let flag = true
if (flag) {
resolve('火锅~')
} else {
reject('获取火锅失败')
}
}, 2000)
})
async function fun() {
let dataTea = await Tea
console.log(dataTea)
let dataHotPot = await HotPot
console.log(dataHotPot)
}
fun()
//奶茶~
//火锅~
let Tea = new Promise((resolve, reject) => {
setTimeout(() => {
let flag = true
if (flag) {
resolve('奶茶~')
} else {
reject('获取奶茶失败')
}
}, 2000)
})
let HotPot = new Promise((resolve, reject) => {
setTimeout(() => {
let flag = false
if (flag) {
resolve('火锅~')
} else {
reject('获取火锅失败')
}
}, 2000)
})
Tea.then((data) => {
console.log(data)
return HotPot
})
.then((data) => {
console.log(data)
})
.catch((err) => {
console.log(err)
})
//奶茶~
//获取火锅失败
let cat = {
name: "miaomiao~",
};
let dog = {
name: "wangwang~",
sayName(food1, food2) {
console.log(`我是${this.name},我喜欢吃${food1}和${food2}`);
},
};
dog.sayName("骨头", "肉");
dog.sayName.call(cat, "鱼", "肉");
dog.sayName.apply(cat, ["鱼", "肉"]);
let newFun = dog.sayName.bind(cat, "鱼", "肉");
newFun();
dog.sayName.bind(cat, "鸡", "鸭")();
//我是wangwang~,我喜欢吃骨头和肉
//我是miaomiao~,我喜欢吃鱼和肉
//我是miaomiao~,我喜欢吃鱼和肉
//我是miaomiao~,我喜欢吃鱼和肉
//我是miaomiao~,我喜欢吃鸡和鸭
function User() {
this.crudContent = function () {
console.log("---..---");
};
}
function Person() {
this.login = function () {
console.log("登录成功");
};
}
function Admin() {
User.call(this);
Person.call(this);
}
var admin = new Admin();
admin.crudContent();
admin.login();
//---..---
//登录成功
let cat = {
name: 'miaomiao',
sayName() {
console.log(this)
function fun() {
console.log(this)
}
fun()
},
}
cat.sayName()
let cat = {
name:"miaomiao",
sayName(){
console.log(this);
setTimeout(function(){
console.log(this);
},1000)
}
}
cat.sayName();
let cat = {
name:"miaomiao!",
sayName(){
let self = this;
setTimeout(function(){
console.log(self.name);
},1000)
}
}
cat.sayName();
let cat = {
name: "miaomiao#",
sayName() {
setTimeout(() => {
console.log(this.name);
}, 1000);
},
};
cat.sayName();
function fun() {
console.log(this);
}
let cat = {
name: "miaomiao~",
};
fun.call(cat);
let cat = {
name: "miaomiao@",
};
let dog = {
name: "wangwang!",
sayName() {
console.log("我是" + this.name);
},
};
dog.sayName();
dog.sayName.call(cat);
function getTea(fn) {
setTimeout(() => {
fn("奶茶~");
}, 2000);
}
function getHotPot(fn) {
setTimeout(() => {
fn("火锅~");
}, 1000);
}
getTea((data) => {
console.log(data);
getHotPot((data) => {
console.log(data);
});
});
let promise = new Promise((resolve) => {
setTimeout(() => {
resolve("奶茶~");
}, 2000);
});
promise.then((data) => {
console.log(data);
});
let m = 20
{
let n = 10
let x = 30
{
let n = 50
console.log(x)
}
}
//30
function outerFun() {
let n = 10
function innerFun() {
console.log(n)
}
return innerFun
}
let fun = outerFun()
fun()
//10
let stu = {
name: 'xiaoming',
age: 10,
bestFriend: {
name: 'xiaohong',
age: 10,
},
}
let stuStr = JSON.stringify(stu)
console.log(typeof stuStr)
console.log(stuStr)
let newObj = JSON.parse(stuStr)
console.log(newObj)
let stu = {
name:"xiaoming",
age:10,
bestFriend:{
name:"xiaohong",
age:10
}
}
function copyObject(obj){
let newObj = {};
for(let i in obj){
if(obj[i] instanceof Object){
newObj[i] = copyObject(obj[i]);
}else{
newObj[i] = obj[i];
}
}
return newObj;
}
let newStu = copyObject(stu);
stu.bestFriend.name = "xiaofen";
console.log(stu);
console.log(newStu);
let stu = {
name: 'xiaoming',
age: 10,
bestFriend: {
name: 'xiaohong',
age: 10,
},
}
function copyObject(obj) {
let newStr = JSON.stringify(obj)
let newObj = JSON.parse(newStr)
return newObj
}
let newStu = copyObject(stu)
stu.bestFriend.name = 'xiaofen'
console.log(stu)
console.log(newStu)
let num1 = new Number(123);
let num2 = new Number(123);
let num3 = 123;
console.log(num1 === num2);
console.log(num1 === num3);
let num = 123;
console.log(Boolean(num));
let m = 0;
console.log(String(m));
let str = "123abc";
let num = parseInt(str);
console.log(num);
let a = "123.456abc";
let b = parseFloat(a);
console.log(b);