标签:问题: value 否则 .class wan 取出 [] 执行 基础知识
原始生成实例对象的方法是通过构造函数:
function Person(name, age) { this.name = name; this.age = age } Person.prototype.sayName = function () { console.log(this.name); } var person = new Person(‘wang‘, 18); person.sayName();
// es6 class Person { constructor(name, age) { this.name = name; this.age = age; } say() { console.log(this.name) } }
console.log(typeof Person); // function console.log(Person === Person.prototype.constructor); // true
console.log(Object.keys(Person.prototype)); // ["sayName", "name"]
console.log(Object.keys(Person.prototype)); // []
console.log(Object.getPrototypeOf(person) === Person.prototype) // true
class Person { constructor(name, age) { this.name = name; this.age = age; } say() { console.log(this.name) } } var person = new Person(‘wang‘, 12); person.say(); var { say } = person; say(); // Uncaught TypeError: Cannot read property ‘name‘ of undefined
class Person { constructor(name, age) { this.name = name; this.age = age; } say() { // console.log(this.name) } get name() { console.log(‘get name‘); } set name(value) { console.log(‘set name = ‘ + value) } } var person = new Person(‘wang‘, 12); person.name = ‘wangpei‘; // set name = wangpei person.name; // get name
Person.write = function(){}
class Person { constructor(name, age) { this.name = name; this.age = age; } say() { // console.log(this.name) } static write() { console.log(this.name); } get name() { console.log(‘get name‘); } set name(value) { console.log(‘set name = ‘ + value) } } var person = new Person(‘wang‘, 12); Person.write(); // Person person.write(); // Uncaught TypeError: person.write is not a function
class Person { sex=33; constructor(name, age) { this.name = name; this.age = age; } } var person = new Person(‘wang‘, 12); console.log(person.sex); // 33
function Person(name, age) { console.log(new.target); // Person函数本身 this.name = name; this.age = age } Person.prototype.sayName = function () { console.log(new.target); } var person = new Person(‘wang‘, 18); person.sayName(); // undefined
// es6 class Person { constructor(name, age) { this.name = name; this.age = age; } say() { console.log(this.name); // console.log(this.name) } static write() { console.log(this.write); } } var person = new Person(‘wang‘, 12); person.say(); class Student extends Person { } var student = new Student(‘yao‘, 24); student.say(); // yao Student.write(); // write函数本身
class Person { constructor(name, age) { this.name = name; this.age = age; } say() { console.log(this.name); // console.log(this.name) } static write() { console.log(this.write); } } var person = new Person(‘wang‘, 12); person.say(); class Student extends Person { constructor() { } } var student = new Student(‘yao‘, 24); // Uncaught ReferenceError: this is not defined
class Person { constructor(name, age) { this.name = name; this.age = age; } say() { console.log(this.name); // console.log(this.name) } static write() { console.log(this.write); } } class Student extends Person { constructor() { super(); this.class = 1 console.log(super.say) } run() { console.log(super.say) } } var student = new Student(‘yao‘, 24); student.run(); // function say(){...}
class Person { constructor(name, age) { this.name = name; this.age = age; } say() { console.log(this.name); // console.log(this.name) } static write() { console.log(this.write); } } class Student extends Person { constructor() { super(); this.class = 1 } run() { console.log(super.say); // function say(){...} console.log(super); // 报错 console.log(super.name); // undefined } static eat() { console.log(super.name); // Person } } var student = new Student(‘yao‘, 24); student.run(); Student.eat();
class Person { constructor(name, age) { this.name = name; this.age = age; } say() { console.log(this.name); } } class Student extends Person { constructor() { super(); this.class = 1 this.name = ‘dong‘ } run() { console.log(super.say()); // dong } } var student = new Student(‘yao‘, 24); student.run();
class Person { constructor(name, age) { this.name = name; this.age = age; } say() { } } class Student extends Person { constructor() { super(); } } console.log(Student.__proto__ === Person); // true console.log(Student.prototype === Person); // false console.log(Student.prototype.__proto__ === Person.prototype); // true
标签:问题: value 否则 .class wan 取出 [] 执行 基础知识
原文地址:http://www.cnblogs.com/aiyunyun/p/7373755.html