该API参考范围页面说:
范围可以从父范围继承.
该开发者指南范围页说:
范围(原型)从其父范围继承属性.
那么,子范围是否始终从其父范围继承原型?有例外吗?当它继承时,它是否总是正常的JavaScript原型继承?
javascript inheritance prototype prototypal-inheritance angularjs
该图再次显示每个对象都有一个原型.构造函数Foo也有自己
__proto__的Function.prototype,它又通过其__proto__属性再次引用到Object.prototype.因此,重复,Foo.prototype只是Foo的一个显式属性,它指的是b和c对象的原型.
var b = new Foo(20);
var c = new Foo(30);
Run Code Online (Sandbox Code Playgroud)
__proto__和prototype属性有什么区别?

这个数字来自这里.
javascript prototype prototypal-inheritance javascript-objects
每个JS意见领袖都说扩展原生对象是一种不好的做法.但为什么?我们是否获得了性能?他们是否害怕有人以"错误的方式"做到这一点,并添加了可枚举的类型Object,几乎破坏了任何对象上的所有循环?
以TJ Holowaychuk的should.js为例.他增加了一个简单的getter来Object,一切工作正常(来源).
Object.defineProperty(Object.prototype, 'should', {
set: function(){},
get: function(){
return new Assertion(Object(this).valueOf());
},
configurable: true
});
Run Code Online (Sandbox Code Playgroud)
这真的很有道理.例如,可以扩展Array.
Array.defineProperty(Array.prototype, "remove", {
set: function(){},
get: function(){
return removeArrayElement.bind(this);
}
});
var arr = [0, 1, 2, 3, 4];
arr.remove(3);
Run Code Online (Sandbox Code Playgroud)
是否有任何反对扩展本机类型的论据?
我用OOP语言编程已超过10年,但我现在正在学习JavaScript,这是我第一次遇到基于原型的继承.通过学习优秀的代码,我倾向于学得最快.什么是正确使用原型继承的JavaScript应用程序(或库)的精心编写的示例?您能否(简要地)描述原型继承的使用方式/原因,我知道从哪里开始阅读?
如果我理解正确,Javascript中的每个对象都继承自Object原型,这意味着Javascript中的每个对象都可以通过其原型链访问hasOwnProperty函数.
在阅读require.js的源代码时,我偶然发现了这个函数:
function hasProp(obj, prop) {
return hasOwn.call(obj, prop);
}
Run Code Online (Sandbox Code Playgroud)
hasOwn是一个参考Object.prototype.hasOwnProperty.写这个函数是否有任何实际的区别
function hasProp(obj, prop) {
return obj.hasOwnProperty(prop);
}
Run Code Online (Sandbox Code Playgroud)
既然我们在这,我们为什么要定义这个功能呢?这只是一个快捷方式和本地缓存属性访问(轻微)性能提升的问题,还是我错过了任何可能在没有此方法的对象上使用hasOwnProperty的情况?
在所有文章中都写道,JavaScript是一种基于原型的语言,这意味着每个对象都有一个原型(或更确切地说,原型链).
到目前为止,我已经尝试了以下代码片段:
var F = function();
F.prototype.member1 = 1;
var object1 = new F();
console.log(object1.member1); // prints 1
Run Code Online (Sandbox Code Playgroud)
我怎样才能访问原型对象object1?有没有以浏览器为中心的方式(我的意思是,不依赖于__proto__财产?看到这个链接,但也许自2010年以来有新的发展)如果我不能,你能分享一下引擎背后的理由吗?
// Don't break the function prototype.
// pd - https://github.com/Raynos/pd
var proto = Object.create(Function.prototype, pd({
"prop": 42
}));
var f = function() { return "is a function"; };
f.__proto__ = proto;
console.log(f.hasOwnProperty("prop")); // false
console.log(f.prop); // 42
console.log(f()); // "is a function"
Run Code Online (Sandbox Code Playgroud)
.__proto__ 是非标准的并且已弃用.
我怎么应该继承原型创建一个对象,但让该对象成为一个函数.
Object.create 返回Object而不是Function.
new Constructor 返回Object而不是Function.
动机: - 跨浏览器finherit
var finherit = function (parent, child) {
var f = function() {
parent.apply(this, arguments);
child.apply(this, arguments);
};
f.__proto__ = parent;
Object.keys(child).forEach(function _copy(key) {
f[key] = child[key]; …Run Code Online (Sandbox Code Playgroud) TL; DR:
我们是否需要原型OO中的工厂/施工人员?我们可以做一个范例切换并完全删除它们吗?
BackStory:
我最近一直在玩JavaScript中的原型OO并发现在JavaScript中完成的99%的OO正在强制使用经典的OO模式.
我对原型OO的看法是它涉及两件事.方法(和静态数据)的静态原型和数据绑定.我们不需要工厂或建造者.
在JavaScript中,这些是包含函数和的对象文字Object.create.
这意味着我们可以将所有内容建模为静态蓝图/原型和数据绑定抽象,最好直接连接到文档样式的数据库中.即,从数据库中取出对象,并通过使用数据克隆原型来创建对象.这意味着没有构造函数逻辑,没有工厂,没有new.
示例代码:
一个伪示例是:
var Entity = Object.create(EventEmitter, {
addComponent: {
value: function _addComponent(component) {
if (this[component.type] !== undefined) {
this.removeComponent(this[component.type]);
}
_.each(_.functions(component), (function _bind(f) {
component[f] = component[f].bind(this);
}).bind(this));
component.bindEvents();
Object.defineProperty(this, component.type, {
value: component,
configurable: true
});
this.emit("component:add", this, component);
}
},
removeComponent: {
value: function _removeComponent(component) {
component = component.type || component;
delete this[component];
this.emit("component:remove", this, component);
}
}
}
var entity = Object.create(Entity, toProperties(jsonStore.get(id)))
Run Code Online (Sandbox Code Playgroud)
小解释:
特定代码很冗长,因为ES5很冗长. …
我想把这个问题作为这个答案的问题,但我似乎无法这样做,我道歉.
扩展子类的默认值反映在超类中.这似乎打败了目的,我更倾向于明确列出子类中的超类'默认值来获取我正在寻找的结构.
var Inventory = Backbone.Model.extend({
defaults: {
cat: 3,
dog: 5
}
});
var ExtendedInventory = Inventory.extend({
});
_.extend(ExtendedInventory.prototype.defaults, {rabbit: 25});
var i = new Inventory();
var ei = new ExtendedInventory();
console.log(i.attributes);
console.log(ei.attributes);
Run Code Online (Sandbox Code Playgroud)
这输出:
{cat: 3, dog: 5, rabbit: 25}
{cat: 3, dog: 5, rabbit: 25}
Run Code Online (Sandbox Code Playgroud)
不是我(也不是我认为,op)想要的:
{cat: 3, dog: 5}
{cat: 3, dog: 5, rabbit: 25}
Run Code Online (Sandbox Code Playgroud) 我有一个"SuperClass","info"作为实例变量."SuperClass"具有"printInfo()"功能."printInfo()"需要访问实例变量"info".我想创建一个"分类"里面也有方法"printInfo()".我想从"子类"的"printInfo()"称之为"超类"的printInfo().
SuperClass = function()
{
this.info = "I am superclass";
console.log("SuperClass:");
};
SuperClass.prototype.printInfo = function(that)
{
console.log("printing from superclass printInfo");
console.log(that.info);
};
SubClass = function(){};
SubClass.prototype = new SuperClass();
SubClass.prototype.printInfo = function()
{
console.log("calling superclass");
this.constructor.prototype.printInfo(this);
console.log("called superclass");
};
var sc = new SubClass();
sc.printInfo();
Run Code Online (Sandbox Code Playgroud)
你可以看到,我路过""作为一个参数printInfo.如果没有"说"参数,"信息"打印为"不确定".就像在以下情况下,"this.info"当此功能从"子类"的对象调用是不确定的.
SuperClass.prototype.printInfo = function()
{
console.log("printing from superclass printInfo");
console.log(this.info);
};
Run Code Online (Sandbox Code Playgroud)
什么是覆盖并调用在JavaScript超类的方法,使函数访问类的实例变量的正确方法?
javascript ×10
prototype ×4
inheritance ×3
oop ×2
angularjs ×1
backbone.js ×1
ecmascript-5 ×1
function ×1
node.js ×1
requirejs ×1