function Person(name) { this.name = name}var p2 = new Person("king")console.log(p2.__proto__)console.log(p2.__proto__.__proto__)console.log(p2.__proto__.__proto__.__proto__)console.log(p2.__proto__.__proto__.__proto__.__proto__)console.log(p2.__proto__.__proto__.__proto__.__proto__.__proto__)console.log(p2.constructor)console.log(p2.prototype)console.log(Person.constructor)console.log(Person.prototype)console.log(Person.prototype.constructor)console.log(Person.prototype.__proto__)console.log(Person.__proto__)console.log(Function.prototype.__proto__)console.log(Function.__proto__)console.log(Object.__proto__)console.log(Object.prototype.__proto__)
Person.prototype
Object.prototype
null
报错
报错
Person
undefined
Function
打印出Person.prototype这个对象里所有的方法和属性
Person
Object.prototype
Function.prototype
Object.prototype
Function.prototype
Function.prototype
null
2
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
3
var F = function () {}Object.prototype.a = function () { console.log("a")}Function.prototype.b = function () { console.log("b")}var f = new F()f.a()f.b()F.a()F.b()
a
报错
a
b
4
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()
4
2
1
5
function Dog() { this.name = "puppy"}Dog.prototype.bark = () => { console.log("woof!woof!")}const dog = new Dog()console.log(Dog.prototype.constructor === Dog && dog.constructor === Dog && dog instanceof Dog)
true
6
var A = { n: 4399 }var B = function () { this.n = 9999}var C = function () { var n = 8888}B.prototype = AC.prototype = Avar b = new B()var c = new C()A.n++console.log(b.n)console.log(c.n)
9999
4400
7
function A() {}function B(a) { this.a = a}function C(a) { if (a) { this.a = a }}A.prototype.a = 1B.prototype.a = 1C.prototype.a = 1console.log(new A().a)console.log(new B().a)console.log(new C(2).a)
1
undefined
2
8
function Parent() { this.a = 1 this.b = [1, 2, this.a] this.c = { demo: 5 } this.show = function () { console.log(this.a, this.b, this.c.demo) }}function Child() { this.a = 2 this.change = function () { this.b.push(this.a) this.a = this.b.length this.c.demo = this.a++ }}Child.prototype = new Parent()var parent = new Parent()var child1 = new Child()var child2 = new Child()child1.a = 11child2.a = 12parent.show()child1.show()child2.show()child1.change()child2.change()parent.show()child1.show()child2.show()