我搜索了这么多链接,并且不能很好地了解经典继承和原型继承之间的区别? 我从中学到了一些东西,但我仍然对这些概念感到困惑. > Benefits of prototypal inheritance over classical?> http://aaditmshah.github.io/why-prototypal-inheritance-matters/ 经典继承// Shape - superclass function Shape() {this.x = 0;this.y = 0; }//superclass method Shape.prototype.move = function(x, y) {this.x += x;this.y += y;console.in...
如何继承/扩展使用Revealing Prototype模式的类?有没有办法使私有变量和函数受到保护? 示例基础对象:myNameSpace.Person = function() {this.name= "";this.id = 0;};myNameSpace.Person.prototype = function(){var foo = function(){//sample private function};var loadFromJSON = function (p_jsonObject) {...};var toJSON = function () {...};var clone = function (p_other) {...};return {loadFromJSON : loadFromJSON...
我试图通过以下方式获得原型继承:// Parent constructor function Parent(obj) {this.id = obj.id || 0;this.name = obj.name || ""; };// Child constructor function Child(obj) {Parent.call(this,obj);this.type = obj.type || ""; }Child.prototype = new Parent;似乎教科书……但是将obj传递给父母和孩子似乎都会导致问题;父母说当孩子尝试通过Child.prototype = new Parent;进行原型时,obj是未定义的.我可以解决这个问题的...
我正在尝试学习JavaScript对象继承.我指的是Shelley Powers的JavaScript Cookbook. 在子类中,您需要调用superclass.apply(this,arguments)来使用其属性.根据这本书,我还需要编写类似subclass.prototype = new superclass(); 但是,我注意到事情没有使用subclass.prototype = new superclass();声明.以下是我的代码. subclass.prototype = new superclass()的目的是什么?var Book = function (newTitle, newAuthor) {var title;var ...
我习惯在我的代码中使用这个模式,我喜欢它:var UserWidget = (function(){var url = "/users",tmpl = "#users li", $tmpl;function load() {$tmpl = $(tmpl);$.getJSON(url, function(json){$.each(json, function(i, v) {appendUser(v); });});}...return {load: load}; })();但是,我有很多“小部件”对象. “ProfileWidget”,“PlayerWidget”等等,每个小部件共享的某些动作.理想情况下,如果我们以面向对象的方式思考...
我正在使用John Resig的简单JavaScript继承,并遇到了一个问题,我正在失去’this’所指的内容.使用此代码:var Runner = Class.extend({ init: function() {this.update();if(!this.interval) {this.interval = setInterval(this.update, this.period * 1000);}},stop: function() {clearInterval(this.interval);},update: function() {this.success()},success: function(){} });var SubRunner = Runner.extend({update: function...
在CoffeeScript中,似乎在实例化子类时不会调用超类构造函数. 有没有解决的办法? 这是一个例子:class Aelement = nullconstructor: ->element = document.createElement "div"hide: =>element.style.display = "none"class B extends Aconstructor: ->@hide() #error!我希望首先调用A的构造函数,然后调用B的构造函数.如果B然后调用hide方法,它应该隐藏在A的构造函数中创建的元素,而不是说该元素为null. 谢谢!解决方法:我想你需要...
我正在尝试使用函数apply将类B扩展到类A,将类A扩展到类Super.以下代码可以正常工作:function Super() {this.talk = function () {alert("Hello");}; }function A() {// instance of A inherits SuperSuper.apply(this); }function B() {// instance of B inherits AA.apply(this); }var x = new B(); x.talk(); // Hello但是,如果我想要A类继承自Super类,而不仅仅是它的实例呢?我试过这个:function Super() {this.talk = functi...
我试图在JavaScript中实现简单的继承概念,而不使用任何外部库.这是我的代码.<script>BaseClass = function () {this.doSomething = function () {console.log("base class");};};DerivedClass1 = function () {var bc = new BaseClass();bc.doSomething = function () {console.log("derived class");};return bc;};DerivedClass2 = function () {var bc = new BaseClass();return bc;}var cls1 = new DerivedClass1();cls1.doSome...
在jQuery库中,我发现了这一行jQuery.extend = jQuery.fn.extend = function() {这种困惑让我感到困惑.我认为一个给定的对象自动提供对其原型方法的访问(换句话说,如果一个方法或var没有在对象本身上解析,则在其原型上尝试,然后在原型的原型上等等). 那么,将功能与对象和原型相关联的原因是什么? (在jQuery fn中是原型的别名).解决方法:神奇的原型是构造函数的属性,而不是对象. jQuery的一个实例将继承原型的扩展,但jQuery需要自己...
我知道我可以在JavaScript中做一个简单的原型继承,如下所示:var Parent = function() { };var Child = function() { }Child.prototype = new Parent(); Child.prototype.constructor = Child;但是,我很想知道如何实现更深层次的遗产?那么多继承怎么可能呢?解决方法:您不能在JavaScript中进行多重继承.只需更进一步,您就可以做更深的继承:var Parent = function() {}; var Child = function() {}; var InnerChild = function() ...
在JavaScript:The Good Parts中,Crockford认为使用伪经典继承模式的一个缺点是它公开暴露实例变量. 例如:var Ball = function(width, color) {this.width = width;this.color = color; }var redBall = new Ball(5, "red"); redBall.width = 12; // Changes width to 12现在,如果我希望球的宽度是私密的呢? 这是我尝试过的:var Ball = function(width, color) {this.getWidth = function() { return width; }this.color = color...
我用VueJS和Vuex构建我的应用程序,当我有多个模块使用相同的数据字段时,我正面临着这个问题.它关于像dat这样的API配置.getUsers ({ state, commit }) {axios.get(urls.API_USER_URL).then( response => {let data = response.data;parseApi(state, data, 'user');}).catch( err => {console.log('getUser error: ', err);}) },其他模块中的另一个功能就像getPosts ({ state, commit }) {axios.get(urls.API_POST_URL).then( respon...
前言 面向对象编程很重要的一个方面,就是对象的继承。A 对象通过继承 B 对象,就能直接拥有 B 对象的所有属性和方法。这对于代码的复用是非常有用的。 大部分面向对象的编程语言,都是通过“类”(class)实现对象的继承。传统上,JavaScript 语言的继承不通过 class(ES6 引入了class 语法),而是通过“原型对象”(prototype)实现。那么在JS中常见的继承方式有几种呢? ?方式一、原型链继承 这种方式关键在于:子类型的原型为父类...
我有一种情况,我需要创建一个从Array继承的新JavaScript对象.我使用以下代码:// Create constructor function. var SpecialArray = function () {};// Create intermediate function to create closure upon Array's prototype. // This prevents littering of native Array's prototype. var ISpecialArray = function () {}; ISpecialArray.prototype = Array.prototype; SpecialArray.prototype = new ISpecialArray(); Specia...