草庐IT

Crockford

全部标签

javascript - Douglas Crockford 所说的 'constructed in a different window or frame' 是什么意思?

DouglasCrockford在编写is_array()测试时说它将无法识别在不同窗口或框架中构造的数组,这是什么意思?varis_array=function(value){returnvalue&&typeofvalue==='object'&&value.constructor===Array;为什么以下内容跨窗口和框架工作?varis_array=function(value){returnvalue&&typeofvalue==='object'&&typeofvalue.length==='number'&&typeofvalue.splice==='function'&

javascript - Crockford 关于构造函数调用模式的代码

下面的代码几乎与道格拉斯·克罗克福德(DouglasCrockford)的精湛著作《JavaScript:好的部分》第29-30页中的一些代码相同。唯一的区别是他像这样添加了get_status属性:Quo.prototype.get_status=function(){this.status=string;}我的问题是为什么他的代码运行正常,但我在下面的小改动会导致出现错误,提示myQuo没有get_status方法?varQuo=function(string){this.status=string;}Quo.get_status=function(){returnthis.sta

javascript - Crockford 的 hanoi 函数(来自 "The Good Parts")

这个问题在这里已经有了答案:HowdoesrecursivealgorithmworkforTowersofHanoi?(2个答案)关闭8年前。目前,我正在阅读道格拉斯·克罗克福德(DouglasCrockford)的书,汉诺塔的功能让我有点头疼。即使在控制台上记录了一些东西,我也无法真正理解发生了什么。这是我添加的功能:varhanoi=function(disc,src,aux,dst){console.log(disc);console.log(src,dst);if(disc>0){hanoi(disc-1,src,dst,aux);console.log('Movedisc'

javascript - 如何判断正在使用哪个 JSON 对象(Crockford 的或其他的)?

我正在使用Crockford的json2.js.当我想进行字符串化时,我会执行JSON.stringify()...效果很好。但是,看过代码的人都知道它遵循现有的JSON对象和属性。我怀疑我遇到的某个问题可能是由于这种尊重。我可以检查JSON对象的属性以查看浏览器是否正在使用Crockford的对象或其他对象?如果能够执行类似alert(JSON.version());的操作就好了 最佳答案 你可以决定一个这样使用:window.JSON||document.write('')这首先检查window.JSON(浏览器支持),如果存在

javascript - 了解 Crockford 的无类 OOP 实现

我一直在阅读有关在JS中执行OOP的不同方法。DouglasCrockford有一种有趣的方法,他似乎根本不使用委派。相反,对我来说,他似乎纯粹利用对象串联作为他的继承机制,但我很难说出发生了什么,我希望有人能提供帮助。这是克罗克福德在他的一次演讲中给出的一个例子。functionconstructor(spec){let{member}=spec,{other}=other_constructor(spec),method=function(){//accessesmember,other,method,spec};returnObject.freeze({method,other}

javascript - 使用 Douglas Crockford 的函数继承在 Javascript 中调用基方法

基本上我如何使用下面的这种模式调用基本方法?varGS={};GS.baseClass=function(somedata){varthat={};that.data=somedata;//Baseclassmethodthat.someMethod=function(somedata){alert(somedata);};returnthat;};GS.derivedClass=function(somedata){varthat=GS.baseClass(somedata);//Overwritingbasemethodthat.someMethod=function(someda

javascript - 如何在 Crockford 的新构造函数模式中共享 "constructor"功能?

我看过来自thisquestiononclass-freeOOP的视频现在好几次了,但我无法将其应用到现实世界的示例中。Crockford的新构造函数模式如下所示:functionconstructor(spec){let{member}=spec,{other}=other_constructor(spec),method=function(){//accessesmember,other,method,spec};returnObject.freeze({method,other,});}其中spec是一个选项散列,生成的对象公开了关闭所有内部成员的方法。忽略解构(因为这可以在当今

javascript - Douglas Crockford 的 Strict Mode Example 是不是错了?

我敢肯定他不是。我只是不明白他的演讲中的一个例子http://youtu.be/UTEqr0IlFKY?t=44mfunctionin_strict_mode(){return(function(){return!this;}());}这不也一样吗?functionin_strict_mode(){return!this;}如果is_strict_mode()是method那么我同意,因为this然后会指向包含方法的对象,例如my_object.in_strict_mode=function(){return(function(){return!this;}());}但为什么他在他的示

javascript - Crockford 原型(prototype)继承的小缺点

只是在JS中尝试不同的继承技术,并且发现了一些关于Crockford的原型(prototype)继承模式的稍微令人不安的事情:functionobject(o){functionF(){}F.prototype=o;returnnewF();}varC,P={foo:'bar',baz:function(){alert("bang");}}C=object(P);一切都很好-除了当你登录到控制台时-对象显示为F。我见过经典的仿真,你可以在其中重新指向构造函数-是否有类似的方法来强制对象(控制台)引用? 最佳答案 问题是它指的是构造函

javascript - Crockford 的 entityify 方法 - 为什么关闭?

在Crockford的JavaScript:TheGoodParts第90页,他的代码如下:String.method('entityify',function(){varcharacter={'':'>','&':'&','"':'"'};returnfunction(){returnthis.replace(/[&"]/g,function(c){returncharacter[c];});};}());console.log("".entityify());关闭并立即调用外部函数是否有充分的理由?以下似乎也有效:String.method('entityi