使用 Object.create 创建对象,super 关键字,class 关键字

ECMAScript 5 中引入了一个新方法:Object.create()。可以调用这个方法来创建一个新对象。新对象的原型就是调用 create 方法时传入的第一个参数:

var a = {a: 1}; 
// a ---> Object.prototype ---> null

var b = Object.create(a);
// b ---> a ---> Object.prototype ---> null
console.log(b.a); // 1 (继承而来)

var c = Object.create(b);
// c ---> b ---> a ---> Object.prototype ---> null

var d = Object.create(null);
// d ---> null
console.log(d.hasOwnProperty); // undefined, 因为d没有继承Object.prototype

使用 class 关键字

ECMAScript6 引入了一套新的关键字用来实现 class。使用基于类语言的开发人员会对这些结构感到熟悉,但它们是不一样的。 JavaScript 仍然是基于原型的。这些新的关键字包括 classconstructorstaticextends, 和 super.

 

"use strict";
class Polygon {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

class Square extends Polygon {
  constructor(sideLength) {
    super(sideLength, sideLength);
  }
  get area() {
    return this.height * this.width;
  }
  set sideLength(newLength) {
    this.height = newLength;
    this.width = newLength;
  }
}

var square = new Square(2);

super 关键字用于访问父对象上的函数。

class Polygon {
  constructor(height, width) {
    this.name = 'Polygon';
    this.height = height;
    this.width = width;
  }
  sayName() {
    console.log('Hi, I am a ', this.name + '.');
  }
}

class Square extends Polygon {
  constructor(length) {
    this.height; // 这里会报错, 必须要先调用 super!
    
    // 这里我们调用父类的构造方法并传入 length
    // 作为 Polygon's 的 width 和 height
    super(length, length);
    
    // Note: 在派生的类中, super() 必须在 'this' 之前调用
    // 如果漏掉,则会造成引用错误。
    this.name = 'Square';
  }

  get area() {
    return this.height * this.width;
  }

  set area(value) {
    this.area = value;
  } 
}

  

调用父类上的静态方法

你也可以用 super 调用父类的 静态方法

class Human {
  constructor() {}
  static ping() {
    return 'ping';
  }
}

class Computer extends Human {
  constructor() {}
  static pingpong() {
    return super.ping() + ' pong';
  }
}
Computer.pingpong(); // 'ping pong'

在对象字面量中使用 super.prop

var obj1 = {
  method1() {
    console.log("method 1");
  }
}

var obj2 = {
  method2() {
   super.method1();
  }
}

Object.setPrototypeOf(obj2, obj1);
obj2.method2(); // logs "method 1"

  

 

 

 

转载于:https://www.cnblogs.com/laneyfu/p/6082782.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import FreeCAD import FreeCADGui from PySide import QtGui, QtCore Gui.activateWorkbench("PartWorkbench") FreeCAD.newDocument() class SphereDialog(QtGui.QDialog): def __init__(self): super().__init__() self.setWindowTitle("Create Sphere") self.create_widgets() def create_widgets(self): layout = QtGui.QVBoxLayout() # Radius widget radius_label = QtGui.QLabel("Radius:") self.radius_spinbox = QtGui.QDoubleSpinBox() self.radius_spinbox.setMinimum(0.1) self.radius_spinbox.setMaximum(1000.0) self.radius_spinbox.setValue(10.0) layout.addWidget(radius_label) layout.addWidget(self.radius_spinbox) # Opacity widget opacity_label = QtGui.QLabel("Opacity:") self.opacity_slider = QtGui.QSlider(QtCore.Qt.Horizontal) self.opacity_slider.setMinimum(0) self.opacity_slider.setMaximum(100) self.opacity_slider.setValue(50) layout.addWidget(opacity_label) layout.addWidget(self.opacity_slider) # Create button create_button = QtGui.QPushButton("Create") create_button.clicked.connect(self.create_sphere) layout.addWidget(create_button) self.setLayout(layout) def create_sphere(self): # Get sphere parameters radius = self.radius_spinbox.value() opacity = float(self.opacity_slider.value() / 100) # Create sphere sphere = FreeCAD.ActiveDocument.addObject("Part::Sphere", "Sphere") sphere.Radius = radius sphere.ViewObject.Transparency = int(round(opacity * 100)) # Show in 3D view sphere.ViewObject.Visibility = True FreeCADGui.SendMsgToActiveView("ViewFit") FreeCADGui.Selection.clearSelection() FreeCADGui.Selection.addSelection(sphere) FreeCADGui.SendMsgToActiveView("ViewFit") # Close dialog self.close() dialog = SphereDialog() dialog.show()代码中执行没有球体出现,请修改代码
最新发布
05-30

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值