- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我倾向于用 C 风格编写我的 Javascript“类”。
在 C# 中(例如)我们这样做
public class Parent {
// stuff
}
public class Child : Parent {
// Protected and public stuff from Parent will be accessible
}
在 JS 中,我通过使用 proto 找到了等效的例子
var namespace = namespace || {};
namespace.Parent = function() {
// Public variables
self.pubVariable = "I am accessible";
// Private variables
var priVariable = "I'm not accessible outside of self";
// ctor
function self() {}
return self;
}
namespace.Child = (function() {
this.__proto__ = new namespace.Parent();
// ctor
function self() {}
self.init = function() {
// Prints the pubVariable
console.log(pubVariable);
};
return self;
})($);
// Call it (statically)
namespace.Child.init();
虽然这只适用于 Webkit 和 Mozilla。我知道这可以通过原型(prototype)以某种方式实现,但不知道如何实现。任何建议表示赞赏。谢谢!
最佳答案
对于父/子类,我会做这样的事情
// your parent class
var Parent = function() {
// parent constructor
console.log("parent constructor!");
// some public properties
this.foo = "foo";
this.bar = "bar";
// a private data member
var secret = "123456";
};
// a parent function
Parent.prototype.something = function() {
console.log("something!");
}
// your child class
var Child = function() {
// call parent constructor
Parent.call(this);
// child constructor
console.log("child constructor!");
// override bar
this.bar = "override!";
};
// the magic!
// child prototype build from parent prototype
Child.prototype = Object.create(Parent.prototype, {constructor: {value: Child}});
示例用法
var c = new Child();
c.something();
// => parent constructor!
// => child constructor!
// => something!
c.foo //=> "foo"
c.bar //=> "override!"
如果您使用的是“命名空间”,则概念是相同的。
编辑
根据您的评论,这里是并添加了演示
var Foo = function(){};
Foo.prototype.hello = function(){ return "hello!"; };
var foo = new Foo();
// call our hello method
// this calls foo.__proto__.hello
foo.hello(); //=> "hello!"
// override `hello` method for this instance
foo.hello = function(){ return "こんにちは"; };
// call our hello method again
// this calls foo.hello because it is defined directly on our instance
// (it has a higher precedence in the lookup chain)
foo.hello(); //=> "こんにちは"
// remove the override
delete foo.hello;
// call our hello method again
// this goes back to calling foo.__proto__.hello
foo.hello(); //=> "hello!"
// remove the method prototype
delete Foo.prototype.hello
// call our hello method one last time
// spoiler: it's gone!
foo.hello(); //=> TypeError: Object [object Object] has no method 'hello'
如您所见,使用 this.something = function(){};
直接在实例上定义方法会失去此功能。我个人更喜欢在原型(prototype)上定义方法,因为它增加了灵 active 。这样,原型(prototype)就真的像蓝图一样工作了。您将获得所有预定义的行为;您可以根据需要进行修改,并随时恢复为原始状态,所有这些都基于每个实例。
还有一件事
在我们的最后一个例子中,我们有一个原型(prototype)方法和一个实例方法覆盖。有没有办法也调用原始方法?让我们看看!
var Foo = function(){};
Foo.prototype.hello = function(){ return "hello!"; };
var foo = new Foo();
foo.hello = function(){ return "こんにちは!"; }
// call override method
foo.hello(); //=> "こんにちは!"
// call original method
Foo.prototype.hello.call(foo); //=> "hello!"
// japanese just one more time...
foo.hello(); //=> "こんにちは!"
这也行,但我从来没有真正需要。我想这样做的好处是您不需要通过这种方式了解原始类(class) :)
// call original method from the instance
foo.__proto__.hello.call(foo); //=> "hello!"
原型(prototype)!
关于javascript - 用原型(prototype)替换 __proto__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17263299/
我的理解是 Object.__proto__ 是 javascript 中的“顶级”原型(prototype)对象。我希望它的 __proto__ 为空,但在谷歌浏览器中(没有尝试过其他浏览器),它不
为什么 Object.__proto__ === Function.__proto__ 在 JavaScript 中? 这样设计的目的是什么? 最佳答案 __proto__ property对象的原型
我一直在研究 prototype chain在 Javascript 中,这个主题让我想知道...是否有一个通用名称或术语来指代原型(prototype)链末尾的所有其他对象? 我正在 GitHub
似乎在 JavaScript (ES6) 类中 super.__proto__ === this.__proto__ . 您能解释一下为什么会出现这种情况吗?该行为在不同浏览器中似乎是一致的,因此我怀
为什么如果F - 简单函数: F.prototype !== F.__proto__ 但是 Function.prototype === Function.__proto__ ? 最佳答案 F.pro
obj.prototype.__proto__ = events.EventEmitter.prototype 我有时会看到上面的代码,我用谷歌搜索了一下,他们说这一行将所有 EventEmitter
曾经能够毫无问题地做到这一点: for (data in dataArray) { // do some stuff }; 现在,它突然迭代这个 __proto__添加到我的 dataArray 的数
我正在阅读 __proto__ 和 prototype 对象之间的区别,但是当我尝试访问 __proto__ 的实例时一个对象,它返回给我 undefined。 下面是我写的代码: function
当一个对象被实例化时,无论是字符串/函数/等等,__proto__属性(property)包括在内。该属性似乎是由 __proto__ 生成的Object.prototype 中的访问器... Obj
__proto__ 已弃用。为什么像谷歌浏览器这样的大型浏览器仍在使用它?有没有计划倾倒它? 最佳答案 在语言/框架中最难做的事情之一实际上是删除已弃用的功能。世界上仍然有大量代码依赖于此功能。删除它
function makeAdder(x) { return function(y) { return x + y; }; } var add5 = makeAdder(5); add
我正在重新从头开始学习 JavaScript/做个人笔记,并遇到了一些我想要一些解释的东西。 谁能解释一下: Object.prototype.hasOwnProperty("__proto__");
下面是一些示例,展示了基于对象定义和创建方式的原型(prototype)继承的不同行为。我区分对象的“原型(prototype)属性”,例如someObject.prototype 和“原型(prot
我正在尝试使用带有继承的揭示模块模式。我似乎让它工作正常,但它使用“__proto__”,我知道它被认为已被弃用。有没有更好的方法来创建继承而不使用“__proto__”? var Person =
函数getBooks已在Author.prototype中定义。但它不能在 Author 对象中使用。当我使用 __proto__ 继承 Person 属性时。为什么Author对象没有getBook
我有这个功能: 函数 Foo(){} 根据这张图片: >> Foo.prototype -> Foo {} 所以如果我写: Foo.prototype.constructor 现在 - 它引
什么时候 __proto__ 有用? 很多浏览器都支持它,但由于不是所有浏览器都支持,所以程序员似乎害怕使用它。我从未在任何代码中看到它(例如库 jQuery 和 backbone.js)。 什么时候
当我创建一个空白对象时: var o = {}; 为什么新建对象时看不到'__proto __'对象,但添加函数时可以? 编辑:为了完整起见,要创建一个真正的空白对象(没有原型(prototype)链
Object.each , Object.keys , ... 当我尝试与作为类属性的对象一起使用时无法正常工作,请检查 this example : var map = {a: 1, b: []};
在 __proto__ 周围使用方括号时在一个对象中,__proto__是可枚举的。未提供方括号时,__proto__是不可枚举的。 例子: obj = {"__proto__": "Hello"}
我是一名优秀的程序员,十分优秀!