带解析
var a = {n: 1};
var b = a;
a.x = a = {n: 2};
console.log(a.x)//undefined
console.log(b.x)//{n:2}
//1.对象赋值是内存地址// example 1
var a={}, b='123', c=123;
a[b]='b';
a[c]='c';
console.log(a[b]);//c
// example 2
var a={}, b=Symbol('123'), c=Symbol('123');
a[b]='b';
a[c]='c';
console.log(a[b]);//b
// example 3
var a={}, b={key:'123'}, c={key:'456'};
a[b]='b';
a[c]='c';
console.log(a[b]);//c
//1.对象中同名属性会被覆盖
//2.对象的属性名称始终会被toString
//3.Symbol永远不相同function changeObjProperty(o) {
o.siteUrl = "http://www.baidu.com"
o = new Object()
o.siteUrl = "http://www.google.com"
}
let webSite = new Object();
changeObjProperty(webSite);
console.log(webSite);
//http://www.baidu.com
//1.在外部被当成参数传入方法的常量不能在函数内改变外部原本值,但如果是对象,则可以添加属性
//2.对象的地址变化var a = 2
function P() {
this.a = 5
this.fn = function() {
console.log(this.a)
}
}
var obj1 = new P()
obj1.fn() //5
var obj2 = new P().fn
obj2() //2
//1.this始终默认指向执行他的地方let obj = {
2: 3,
3: 4,
5: 6,
length: 2,
push: Array.prototype.push
}
obj.push(1)
obj.push(2)
console.log(obj)
//输出{2: 1, 3: 2, 5: 6, length: 4, push: ƒ}
// 为什么?
// 首先,明确Array.prototype.push内部怎么实现的,所以我们手写一个
// Array.prototype.push = function(val){
// this[this.length] = val
// this.length++
// return this.length
// }
// 然后,带入计算
// 第一步:obj.push(1),解析: obj[obj.length] = 1 -> obj[2] = 1,length = 3
// 第二部:obj.push(2),解析: obj[obj.length] = 2 -> obj[3] = 2,length = 4let nums = [1, "12", 3]
nums[4] = 4
nums.unshift(5)
nums.pop()
console.log(nums.length)
//考察数组方法扩展,数组跨位置添加class Animal {
constructor(name) {
this.props = {
name
}
}
fly() {
function canFly() {
return this.props.name === "birds";
}
if (canFly()) {
console.log("i can")
} else {
throw new error("no")
}
}
}
const animal = new Animal('birds')
try {
animal.fly()
} catch (e) {
console.log(e)
}
//错误处理,报错机制var i = 5;
function fn(i) {
return function(n) {
console.log(n + (++i))
}
}
var f = fn(1)
f(2) //4 i=5
fn(3)(4) //8 i=5
fn(5)(6) //12 i=5
f(7) //10 i=5
console.log(i) //5
//++i的意义,闭包值常驻let a = 0,
b = 0;
function A(a) {
A = function(b) {
console.log(a + b++)
}
console.log(a++)
}
A(1) //1
A(2) //4
//误区较大
//第一次A(1)执行时,首先将函数A覆盖了,其次执行后续a++
//第二次A(2)执行时,由于第一次的函数覆盖,导致结构变化,但第一次所定义在函数体内的变量a仍然存在只有题
console.log(c);
var c;
function c(a) {
console.log(a);
var a = 3;
function a(){
}
}
c(2);
//----------------------------//
var c = 1;
function c(c) {
console.log(c);
var c = 3;
}
console.log(c);
c(2);
//----------------------------//
var name = 'erdong';
(function () {
if (typeof name === 'undefined') {
var name = 'chen';
console.log(name);
} else {
console.log(name);
}
})();
//----------------------------//
var a = 10;
function test() {
a = 100;
console.log(a);
console.log(this.a);
var a;
console.log(a);
}
test();
//----------------------------//
if (!('a' in window)) {
var a = 1;
}
console.log(a);
//----------------------------//
(function() {
var a = b = 3;
})();
console.log(typeof a === 'undefined');
console.log(typeof b === 'undefined');
//----------------------------//
var a = (function(foo){
return typeof foo.bar;
})({foo:{bar:1}});
console.log(a);
//----------------------------//
function Person() {
getAge = function () {
console.log(10)
}
return this;
}
Person.getAge = function () {
console.log(20)
}
Person.prototype.getAge = function () {
console.log(30)
}
var getAge = function () {
console.log(40)
}
function getAge() {
console.log(50)
}
Person.getAge();
getAge();
Person().getAge();
new Person.getAge();
getAge();
new Person().getAge();
//----------------------------//
console.log(typeof a);
function a() {}
var a;
console.log(typeof a);
//----------------------------//
var x = 1;
if(function f(){}){
x += typeof f;
}
console.log(x);
//----------------------------//
var x = 10;
function fn() {
console.log(x);
}
function show(f) {
var x = 20;
f();
}
show(fn);
//----------------------------//
function foo1() {
return {
bar: "hello"
};
}
function foo2() {
return
{
bar: "hello"
};
}
console.log(foo1());
console.log(foo2());
//----------------------------//
console.log(Array(3));
console.log(Array(2,3));
//----------------------------//
var a=[1, 2, 3];
console.log(a.join());
//----------------------------//
var a = [3];
var b = [1];
console.log(a - b); 