我刚开始阅读Douglas Crockford的" Javascript The Good parts ",他解释了有关扩充基本类型的内容.
Function.prototype.addMethod=function(name,func) {
this.prototype[name]=func;
return this;
};
Run Code Online (Sandbox Code Playgroud)
执行此操作后的那一刻,addMethod可用于所有基本对象,如String,Number等.这让我感到困惑
当我没有将它添加到Object.prototype时,为什么会发生这种情况?
为什么在Function.prototype中添加方法会反映在所有基本对象中?
鉴于这种非常熟悉的原型构造模型:
function Rectangle(w,h) {
this.width = w;
this.height = h;
}
Rectangle.prototype.area = function() {
return this.width * this.height;
};
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么呼叫new Rectangle(2,3)持续比Rectangle(2,3)没有'new'关键字呼叫快10倍?我会假设因为new会通过涉及原型来增加函数执行的复杂性,所以它会更慢.
例:
var myTime;
function startTrack() {
myTime = new Date();
}
function stopTrack(str) {
var diff = new Date().getTime() - myTime.getTime();
println(str + ' time in ms: ' + diff);
}
function trackFunction(desc, func, times) {
var i;
if (!times) times = 1;
startTrack();
for (i=0; i<times; i++) {
func();
}
stopTrack('(' + times …Run Code Online (Sandbox Code Playgroud) 我喜欢在javascript中,我可以创建一个函数,然后向该函数添加更多的方法和属性
myInstance = function() {return 5}
myInstance.attr = 10
Run Code Online (Sandbox Code Playgroud)
我想创建一个类来生成这些对象.我假设我必须从Function基类继承.
换句话说,我想:
var myInstance = new myFunctionClass()
var x = myInstance()
// x == 5
Run Code Online (Sandbox Code Playgroud)
但我不知道如何创建myFunctionClass.我尝试了以下,但它不起作用:
var myFunctionClass = function() {Function.call(this, "return 5")}
myFunctionClass.prototype = new Function()
myInstance = new myFunctionClass()
myInstance()
// I would hope this would return 5, but instead I get
// TypeError: Property 'myInstance' of object #<Object> is not a function
Run Code Online (Sandbox Code Playgroud)
我还试过这里找到的更复杂(也更合适?)的继承方法:如何在JavaScript中"正确"创建自定义对象?,没有更多的运气.我也尝试使用node.js中的util.inherits(myFunctionClass,Function).仍然没有运气
我已经筋疲力尽了谷歌,因此我觉得我必须遗漏一些基本或明显的东西.非常感谢帮助.
javascript inheritance function-object prototypal-inheritance
我在Mozilla polyfill中看到fn.bind()这样的:
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
// other code omitted here...
};
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我们要检查...的类型this...因为如果我们说fn.bind()并且fn是一个函数,那么它将起作用,如果fn不是一个函数,那么fn.bind将永远不会达到Function.prototype.bind原型继承.那么为什么我们要检查类型this?
我有一些javascript在第三方的网站上运行,这需要我暂时向Array类型添加一个函数,例如
Array.prototype.foo = function() { alert("foo's for everyone!"); };
Run Code Online (Sandbox Code Playgroud)
我想成为一名优秀的开发人员,尽可能不污染其他人的代码类型系统,所以一旦不再需要该功能,我想再次删除该功能.这可能吗?
在过去的两天里,我一直在敲打这个.由于某种原因,骨干是在继承的子模型之间共享父实例数据.下面是一个例子:
var Base = Backbone.Model.extend({
index : []
});
var Group = Base.extend({
initialize : function() {
this.index.push('from group');
}
});
var User = Base.extend({
initialize : function() {
this.index.push('from user');
}
});
var user = new User();
console.log(user.index); // ['from user']
var group = new Group();
console.log(group.index) // ['from user', 'from group']
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是:
console.log(user.index); // ['from user']
console.log(group.index) // ['from group']
Run Code Online (Sandbox Code Playgroud)
任何见解?
谢谢!马特
我正在尝试升级JavaScript(我在工作中没有使用它),并且很好地包围了Constructor函数以及如何实例化从它们继承属性的新对象.
但通常要真正学到一些东西,我需要在我正在研究的实际项目中使用它并在行动中看到它.
问题是,我正在阅读的所有内容都使用这些示例来解释继承:
function Apple (type) {
this.type = type;
this.color = "red";
this.getInfo = getAppleInfo;
}
Run Code Online (Sandbox Code Playgroud)
要么
function Car( model, year, miles ) {
this.model = model;
this.year = year;
this.miles = miles;
this.toString = function () {
return this.model + " has done " + this.miles + " miles";
};
}
Run Code Online (Sandbox Code Playgroud)
要么
function makeBaby(parent, name) {
var baby = Object.create(parent);
baby.name = name;
return baby;
}
Run Code Online (Sandbox Code Playgroud)
正如您可能想象的那样,这些例子(水果,汽车和父母)对于学习概念的目的肯定是有帮助的,但实际上并没有将它们付诸实践.
有没有人举例说明原型继承如何在生产级Web应用程序中起作用?
如果变量在需要时在函数中不可用,那么它将在作用域链中查找(这是一个闭包),但有时它会在原型链中进行搜索.我正试着把头包裹起来,当时正在发生这种情况.我想知道是否有人可以为我清除薄雾,或者让我参考一些专门讨论这个主题的文献.
例如,我是否正确地说: - 对象和因此与上下文绑定的公共变量(this)总是在原型链中查找? - 私有变量总是在作用域链中查找(即执行上下文中的函数链)? - 当程序同时查看两者中的任何一种情况时是否存在?
我测试了三种不同的场景(范围链查找,原型查找和没有查找),但不幸的是它没有足够的帮助到达底部.
var Food = function(){
var t = 1; // for closure
this.timeToPrepare = function(){ // Scope chain lookup
console.log(t * 3);
};
this.timeToMake = function(){ // This is looked up in the prototype chain
console.log(this.t * 3);
};
this.timeToEat = function(t){ //No lookup
console.log(t * 3);
};
};
Food.prototype.t = 2;
(function(){
var pizza = new Food;
pizza.timeToPrepare(); //3
pizza.timeToMake(); //6
pizza.timeToEat(3); //9
})();
Run Code Online (Sandbox Code Playgroud)

谢谢!
在浏览器(至少chrome)函数是实例 Function
setTimeout instanceof Function
// true
Run Code Online (Sandbox Code Playgroud)
但是在节点中,它们不是
setTimeout instanceof Function
// false
Run Code Online (Sandbox Code Playgroud)
那么setTimeout如果不是Function什么构造函数呢?
MDN对于修改代码中的原型有一个巨大的可怕警告:
根据
[[Prototype]]现代JavaScript引擎如何在每个浏览器和JavaScript引擎中优化属性访问,非常慢的操作,改变对象的性质.对改变继承的性能的影响是微妙和遥远的,并不仅仅局限于在Object.setPrototypeOf(...)语句中花费的时间,而是可以扩展到任何可以访问任何[[Prototype]]已被更改的对象的代码 .如果您关心性能,则应避免设置[[Prototype]]对象.而是[[Prototype]]使用所需的使用创建一个新对象Object.create().
我想知道是否有任何情况你可以修改对象的原型而不会导致级联的优化效果,从而破坏程序的性能.好像应该有.例如,如果您只是在创建对象之后(以及在其他任何东西使用之前)修改原型.但我想这是依赖引擎的.
那么有谁知道是否有有效的方法来修改对象的原型?
编辑:这个问题的动机来自于创建实际继承的对象的愿望Function.我知道如何做到这一点的唯一方法是修改函数的原型.请参阅Raynos答案的最底部:javascript类继承自Function类
javascript inheritance prototype prototypal-inheritance prototype-chain
javascript ×10
inheritance ×3
backbone.js ×1
class ×1
closures ×1
constructor ×1
node.js ×1
prototype ×1
scope-chain ×1