将以下代码发布到Babel REPL中class Test {}class Test2 extends Test {}你得到这个继承功能function _inherits(subClass, superClass) {if (typeof superClass !== "function" && superClass !== null) {throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);}subClass.prototype = Object.create(superClass && superClass.prototype, {constructor: {value: subClass,enumera...
//普通对象 //函数对象(有原型 prototy 的属性) //原型的应用 继承 function Amial(){this.type = 小于 } function cat(name){this.name = name } cat.prototype = new Amial() var cat1 = new cat(小张) console.log(cat1.name,cat1.type)//构造器 继承 缺点:把父元素的属性都复制了 function Amial(){this.type = 动物 } function amm(name){Amial.apply(this) //调用Amial构造器 Amial == amm//Amial.call(this,x,y,z) Ami...
我正在使用protypal继承,我想在基类上调用重写方法.在PHP中,我可以使用parent :: functionName来完成此操作.这可能是使用JavaScript protypal继承吗? 请考虑以下示例:var A = function(){this.doSomething = function(){console.log('doSomething in A');};};var B = function() {this.doSomething = function(){//I would like to call A.doSomething()//I tried this.prototype.doSomething() and A.doSomething.call(this), b...
我过去几个小时一直在研究原型继承,但是我对如何应对这个问题留下了相互矛盾/不清楚的答案.它似乎适用于我现在如何使用它.Paper.Component = function(){}; // useless unless I can solve problem mentioned below? Paper.Component.prototype = {isInBounds: function(x, y){// ...},setPosition: function(x, y){// ...},setSize: function(w, h){// ...} };Paper.Button = function(x, y, w, h, text){// ... } Paper.Button.p...
在我的server.js中,我包含了下划线库.var _ = require('underscore')我的路线是这样的:// require routes require('./routes/document');在文档路径中,我想使用下划线.但似乎_ var不是继承/在文档范围内.这是否意味着我必须在每个必需的路线上设置_ var?或者有更聪明的方法来做到这一点? 谢谢.解决方法:是的,您应该在需要它的文件中设置_. 或者,您可以通过删除var部分将其置于全局范围内._ = require('underscore'); require('....
我想知道以下是否符合ES6规范:class X {constructor(name) {this._name = name;}get name() {return this._name;}set name(name) {this._name = name + "X";} }class Y extends X {constructor(name) {super(name);}set name(name) {super.name = name;this._name += "Y";} }想法是让y = new Y(“”); y.name =“hi”应该导致y.name ===“hiXY”为真. 据我所知,这在启用了ES6标志的Chrome中无效.它也不能使用带有es2015标志的Babe...
我正在创建两个对象(继承),都从Base继承.第二个对象的属性赋值会覆盖第一个对象中的值. 有什么想法吗?如何进行适当的继承,以便Base类将包含其继承后代的公共成员,但后代可以分配自己的值而不会相互干扰.var testParams1 = { title: "john" }; var testParams2 = { title: "mike" };Function.prototype.inheritsFrom = function (baseClass) {this.prototype = new baseClass;this.prototype.constructor = this;this.prototype.ba...
我想使用Javascript来切换大部分HTML的可见性,其中还包含display:< value>属性. 问题是使用Javascript设置外部显示属性也设置内部显示属性. 鉴于以下CSS:.zapfenintermeddivisionrow {vertical-align:top;display:none; }.divisionColumn[data-division=true][data-boxed=true] {border: 1px solid black;display: inline-block;float: left; }HTML:<tr class='zapfenintermeddivisionrow'> <td class='zapfendividendintermed'...
我不太熟悉javascript继承,我试图让一个对象从另一个继承,并定义自己的方法:function Foo() {} Foo.prototype = {getColor: function () {return this.color;}, }; function FooB() {} FooB.prototype = new Foo(); FooB.prototype = {/* other methods here */ };var x = new FooB().getColor();但是,第二个覆盖第一个(FooB.prototype = new Foo()被取消).有什么方法可以解决这个问题,还是我朝错误的方向走? 在此先感谢,抱歉任何...
我的问题是关于维护其父对象的原型链的子对象. 在John Resig的高级Javascript幻灯片(http://ejohn.org/apps/learn/#76)中,他写道,为了维护子对象的原型链,您必须实例化一个新的父对象. 然而,通过几个快速测试,我注意到通过将子对象原型设置为等于父对象原型来维护原型链. 任何澄清将不胜感激! 原始代码function Person(){} Person.prototype.dance = function(){};function Ninja(){}// Achieve similar, but non-inheritable, re...
我有一个基本服务和两个继承服务:@Injectable({ providedIn: 'root' }) export class BaseService {foo(src?: string){return `speaking from ${src || 'BaseService'}`;} }@Injectable({ providedIn: 'root' }) export class SomeService extends BaseService {foo(){return super.foo('SomeService')} }@Injectable({ providedIn: 'root' }) export class AnotherService extends BaseService {foo(){return super.foo('Another...
我正在学习面向对象的Javascript的一些方面.我遇到了这个片段var Person = function(firstName, lastName) {this.lastName = lastName;this.firstName = firstName; };Object.defineProperties(Person.prototype, {sayHi: {value: function() {return "Hi my name is " + this.firstName;}},fullName: {get: function() {return this.firstName + " " + this.lastName;}} });var Employee = function(firstName, lastName, positio...
我知道有很多类似的问题都有很多很好的答案.我试着看一下经典的继承方法,或者那些闭包方法等.不知怎的,我认为它们或多或少都是“黑客”方法,因为它并不是javascript的设计目的. (如果我错了,欢迎任何人纠正我).好吧,只要它有效,我满足于经典的继承模式,如:PARENTClass = function (basevar) { do something here; }; PARENTClass.prototype = { a: b, c: d}; // prototype is auto gen // Inheritance goes here CHILDClass = fun...
我有一种情况需要检查构造函数(X)在其原型链中是否有另一个构造函数(Y)(或者是Y本身). 执行此操作的最快方法可能是(new X())instanceof Y.在这种情况下,这不是一个选项,因为有问题的构造函数可能会在没有有效参数的情况下实例化. 我考虑的下一个方法是:const doesInherit = (A, B) => {while (A) {if (A === B) return true;A = Object.getPrototypeOf(A);}return false; }这是行得通的,但我无法摆脱这种感觉,即我错过了一些更简单...
实例化对象 function Animal(){this.name=null;this.sleep=function(){return "睡觉";};this.eat=function(){return "吃"; } } var animal=new Animal(); console.log(animal);//原型链继承 _proto_ function Dog(){this.type="犬"; } Dog.prototype=new Animal(); Dog.prototype.color="red"; //想为子类添加属性要放在原型链继承之后进行 否则会被覆盖 var dog=new Dog(); console.log(dog); //dog将继承Animal的属性 conso...