ES6中类的使用
class定义类的方式
class Person {
}
console.log(Person.prototype.constructor);
console.log(Person.prototype.__proto__ === Object.prototype)
console.log(Function.__proto__ === Function.prototype)
console.log(typeof Person)
var p = new Person()
console.log(p.__proto__ === Person.prototype)
class的构造方法
class Person {
constructor(name, age) {
this.name = name
this.age = age
}
}
var p1 = new Person('Fhup', 18)
var p2 = new Person('Fhf', 20)
console.log(p1.name);
console.log(p2.name);
class中方法的定义
class Person {
constructor(name) {
this.name = name
this._address = '北京市'
}
eat() {
console.log(this.name + ' 在吃汉堡!');
}
get address() {
console.log('进行其他的操作!');
return this._address
}
set address(value) {
this._address = value
}
static createNewPerson(){
return new Person('bobo')
}
}
var p1 = new Person('Fhup')
p1.eat()
console.log(p1.address)
p1.address = '宝鸡市'
console.log(p1.address)
var newperson = Person.createNewPerson()
console.log(newperson)
console.log(Object.getOwnPropertyDescriptors(Person.prototype))
class中实现继承
class Father {
constructor(name, age){
this.name = name
this.age = age
}
eat(){
console.log(this.name + ' eat~');
}
static xxx(){}
}
class Son extends Father {
constructor(name, age, toy){
super(name, age)
this.toy = toy
}
play(){
console.log(this.name + ' play gun!');
}
eat(){
super.eat()
console.log(this.name + ' eat milk~');
}
}
var s1 = new Son('Fhup', 18, '枪')
console.log(s1);
s1.eat()
s1.play()
ES6转ES5的代码
class Person{
constructor(name){
this.name = name
}
eat(){
console.log(eat)
}
}
"use strict";
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
Object.defineProperty(Constructor, "prototype", { writable: false });
return Constructor;
}
var Person = (function () {
function Person(name) {
_classCallCheck(this, Person);
this.name = name;
}
_createClass(Person, [
{
key: "eat",
value: (function (_eat) {
function eat() {
return _eat.apply(this, arguments);
}
eat.toString = function () {
return _eat.toString();
};
return eat;
})(function () {
console.log(eat);
})
}
]);
return Person;
})();
ES6转ES5的继承代码
class Father {
constructor(name, age) {
this.name = name
this.age = age
}
say() {
console.log(this.name + ' say~');
}
}
class Son extends Father {
constructor(name, age, toy) {
super(name, age)
this.toy = toy
}
play(){
console.log(this.name + ' play computer~')
}
}
"use strict";
function _typeof(obj) {
"@babel/helpers - typeof";
return (
(_typeof =
"function" == typeof Symbol && "symbol" == typeof Symbol.iterator
? function (obj) {
return typeof obj;
}
: function (obj) {
return obj &&
"function" == typeof Symbol &&
obj.constructor === Symbol &&
obj !== Symbol.prototype
? "symbol"
: typeof obj;
}),
_typeof(obj)
);
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: { value: subClass, writable: true, configurable: true }
});
Object.defineProperty(subClass, "prototype", { writable: false });
if (superClass) _setPrototypeOf(subClass, superClass);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf =
Object.setPrototypeOf ||
function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _createSuper(Derived) {
var hasNativeReflectConstruct = _isNativeReflectConstruct();
return function _createSuperInternal() {
var Super = _getPrototypeOf(Derived),
result;
if (hasNativeReflectConstruct) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
return call;
} else if (call !== void 0) {
throw new TypeError(
"Derived constructors may only return object or undefined"
);
}
return _assertThisInitialized(self);
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called"
);
}
return self;
}
function _isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Boolean.prototype.valueOf.call(
Reflect.construct(Boolean, [], function () {})
);
return true;
} catch (e) {
return false;
}
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf
? Object.getPrototypeOf
: function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
Object.defineProperty(Constructor, "prototype", { writable: false });
return Constructor;
}
var Father = (function () {
function Father(name, age) {
_classCallCheck(this, Father);
this.name = name;
this.age = age;
}
_createClass(Father, [
{
key: "say",
value: function say() {
console.log(this.name + " say~");
}
}
]);
return Father;
})();
var Son = (function (_Father) {
_inherits(Son, _Father);
var _super = _createSuper(Son);
function Son(name, age, toy) {
var _this;
_classCallCheck(this, Son);
_this = _super.call(this, name, age)
_this.toy = toy;
return _this;
}
_createClass(Son, [
{
key: "play",
value: function play() {
console.log(this.name + " play computer~");
}
}
]);
return Son;
})(Father);
创建类继承自内置类
class Fhup extends Array {
firstItem() {
console.log(this[0], this.at(2))
}
}
var f = new Fhup(333, 666, 999)
f.firstItem()
f.push('12321312')
console.log(f)
JS实现混入效果
class Top {
}
function MixinFn(baseClass) {
class newClass extends baseClass{
run() {
console.log('run~~~');
}
}
return newClass
}
function MixinFn2(baseClass) {
return class extends baseClass{
eat() {
console.log('eat~~~');
}
}
}
var newClass = MixinFn(MixinFn2(Top))
var nc = new newClass()
nc.eat()
nc.run()
传统面向对象多态 ts文件
class Shape {
constructor() {}
getArea() {}
}
class Rectangle extends Shape {
x: number;
y: number;
constructor(x: number, y: number) {
super();
this.x = x;
this.y = y;
}
getArea() {
return this.x * this.y;
}
}
class Circle extends Shape {
r: number;
constructor(r: number) {
super();
this.r = r;
}
getArea() {
return this.r * 2 * Math.PI;
}
}
function calcArea(shape: Shape) {
console.log(shape.getArea());
}
calcArea(new Circle(3));
calcArea(new Rectangle(3, 33));
export {};
JS面向对象多态
function Fn(foo) {
foo.say()
}
var obj = {
name: 'Fhup',
say: function() {
console.log('456');
}
}
class Person {
say() {
console.log('123');
}
}
Fn(obj)
var p = new Person()
Fn(p)