简体   繁体   English

有人可以向我解释继承如何在javascript中工作

[英]can somebody explain me how inheritance works in javascript

can somebody please explain me How Inheritance works in javascript (JS), i have gone through many Tutorials, but I'm not able to understand what it is achieving.. even after night and day no result.. 有人可以向我解释一下继承是如何在javascript(JS)中工作的,我已经看过很多教程,但是我无法理解它正在实现什么。

what i have gone through is : 我经历过的是:

console.log('Initialized..')

function Animal() {
    this.species = "animal";
}

Animal.prototype.category = function (){
    alert(this.species);
}

function Dog(){
    this.Animal();
}

copyPrototype(Dog, Animal)
var d = new Dog();
d.category();

Referenced Tutorial from http://www.sitepoint.com/javascript-inheritance/ 来自http://www.sitepoint.com/javascript-inheritance/的参考教程

In my opinion this: http://www.letscodejavascript.com/v3/episodes/lessons_learned/12 is the best resource on understanding the mechanics we abuse when trying to get classical OOP working in JavaScript. 在我看来,这是http://www.letscodejavascript.com/v3/episodes/lessons_learned/12是了解理解试图使经典OOP在JavaScript中工作时所滥用的机制的最佳资源。

The following is what I do when someone tells me I must use OOP in JS. 以下是当有人告诉我必须在JS中使用OOP时的操作。 It's probably something I've picked up online somewhere. 这可能是我在某处在线购买的东西。 Anyways, here goes: 无论如何,这里是这样:

/// Make this script available somewhere
var extendsClass = this.extendsClass || function (d, b) {
    function __inheritedProto() { this.constructor = d; }
    __inheritedProto.prototype = b.prototype;
    d.prototype = new __inheritedProto();
}

var Animal = (function() {
    function Animal(data) {
        this.value = data;
    }
    Animal.prototype.method = function() {
        return this.value;
    };
    return Animal;
})();

var Dog = (function(_super) {
    extendsClass(Dog, _super);
    function Dog() {
        _super.apply(this, arguments);
    }
    Dog.prototype.method2 = function() {
        return this.value * 2; //do something else
    };
    return Dog;
})(Animal);

/// Create some instances
var animal = new Animal(1);
var dog = new Dog(2);


/// Call some methods
animal.method();  // 1
dog.method(); // 2
dog.method2();// 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以向我解释这个javascript语句吗? - Can somebody explain this javascript statement to me? 有人可以向我解释此正则表达式吗? - Can somebody explain this RegEx to me? 有人能解释一下 John Resig 的pretty.js JavaScript 是如何工作的吗? - Can somebody explain how John Resig's pretty.js JavaScript works? 有人可以解释这个Javascript方法吗? - Can somebody explain this Javascript method? 有人可以向我解释逻辑“OR”运算符的范围在 Javascript 中是如何工作的吗? - Can someone explain to me how the scope of the logical "OR" operator works in Javascript? 有人可以解释javascript算术表达式如何工作吗? - Can somebody explain how javascript Arithmetic expressions work? JavaScript,不能在乒乓球中画出拨片和球。 有人可以解释一下为什么吗? - JavaScript, can't draw paddles and ball in pong. Can somebody please explain me why? 在 JavaScript 中重新分配全局变量 - 有人可以向我解释为什么 currentAcc 保持未定义 - Reassigning global variable in JavaScript - Can somebody explain to me why currentAcc stays undefined 有人可以向我解释这个功能是如何工作的吗? - Can someone explain to me how this function works? 有人可以解释我在这个JavaScript中的错误所在吗? - Can somebody explain where my mistake is in this JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM