jjzjj

function-prototypes

全部标签

javascript - 为 Array.prototype 添加一个 getter

我想向Array.prototype添加一个getter以获取数组的最后一个元素。我是这样做的:Object.defineProperty(Array.prototype,'last',{get:function(){returnthis[this.length-1];}});这适合内存吗?我担心的是如果您实例化10000个对象:我希望我的内存中只有1个函数我担心我的内存中可能有10000*1=10000个函数我的目标是像这样使用它:constarr=[{},{},{},{}];arr.last===arr[arr.length-1]; 最佳答案

javascript - 使用 forEach、[].forEach.call(...) 或 Array.prototype.slice.call(...).forEach 迭代类似数组的对象?

我们可以使用以下两种方法实现类数组对象的迭代:letarrayLike=document.getElementsByClassName('dummy');[].forEach.call(arrayLike,(e)=>{console.log(e);});Test1Test2或者先使用slice将类数组对象转换为数组:letarrayLike=document.getElementsByClassName('dummy');Array.prototype.slice.call(arrayLike).forEach((e)=>{console.log(e);});Test1Test2哪个更

javascript - 为什么 function.apply() 不能在 IE 中跨文档边界工作?

我在IE中看到一些奇怪的行为,试图通过function.apply()调用另一个页面中的函数。这是一个简单的测试用例:test1.html:varopened=null;functionapplyNone(){opened.testFunc.apply(opened);}functionapplyArgs(){opened.testFunc.apply(opened,["appliedarray"]);}functioncall(){opened.testFunc("calleddirectly");}functionremoteApply(){opened.testApply(["u

javascript - 从需要的文件扩展 Node.js 中的 Array.prototype

我将以下内容保存在test.js中。它在浏览器中成功扩展了Array,但它似乎不适用于node和require。有人可以解释这里出了什么问题吗?(function(){Array.prototype.max=function(){returnconsole.log("Array.prototype.max");};returnArray.max=function(){returnconsole.log("Array.max");};}).call(this);然后,从终端:>My-MacBook-Pro:me$node>vartest=require("./test")>[1,2,3]

javascript - Javascript 的 Function.toString() 的逆运算

对于Javascript应用程序,我需要能够让用户保存对象的状态。这涉及保存一组以前动态创建或通过GUI创建的自定义函数,并在以后加载这些存储的函数。本质上,我需要序列化和反序列化函数。现在我通过使用Function对象的.toString()方法实现序列化部分:func.toString().replace('"','\"').replace(/(\r)/g,'').replace(/(\n)/g,'').replace(/(\t)/g,'')这给了我这样美丽的“序列化”函数(注意该函数未命名):"function(someParameter){this.someFunctionNa

javascript - 使用原型(prototype)的 JavaScript 类

我有一个问题,我想创建一个JavaScript类:functionCalculatore(txt,elements){this.p=newProcessor();this.output=txt;$(elements).click(this.clickHandler);}Calculatore.prototype.clickHandler=function(){varelement=$(this);//CodeHere//"this"containstheelement.//ButwhatifIwanttogetthe"output"var?//ItriedwithCalculatore

javascript - 如何在 node.js 中使用原型(prototype)

我在nodejs上编写了我的第一个模块。我需要从谷歌缓存中解析我的网站。帖子是表格帖子的map。当我尝试使用此模块时出现此错误:“类型错误:无法设置未定义的属性‘原型(prototype)’”如何修复这个错误?这是我的代码:module.exports=functionPost(documentDOM,options){this.opts=$.extend({id:0,author_id:0},options);this.doc=documentDOM;this.post={id:0,name:'',alt_name:'',notice:'',content:'',author:'',

Javascript 原型(prototype)行为

我有一个方法可以让我在创建新对象时选择原型(prototype)对象(从“Javascript:TheGoodParts”一书复制):Object.create=function(o){varF=function(){};F.prototype=o;returnnewF();}现在说,我有一个对象:varcar={model:"Nissan"};然后我使用“Create”方法基于这个对象创建了一个新对象:varcar1=Object.create(car);然后我可以向car添加一个属性,它会动态地添加到car1(动态原型(prototype))。例如:car.year=2011;//

javascript - 反序列化后重新应用 JS Prototype 函数

给定以下代码:functionPerson(firstName,lastName){this.FirstName=firstName;this.LastName=lastName;}Person.prototype.showFullName=function(){returnthis.FirstName+""+this.LastName;};varperson=newPerson("xx","xxxx");varjsonString=JSON.stringify(person);varthePerson=JSON.parse(jsonString);我的目标是能够对Person调用“s

javascript - 原型(prototype)链接和 Object.create 之间的区别

我想知道__proto__和Object.create方法之间的区别。举个例子:varob1={a:1};varob2=Object.create(ob1);ob2.__proto__===ob1;//TRUE这意味着Object.create方法创建一个新对象并将__proto__链接设置为作为参数接收的对象。为什么我们不直接使用__proto__链接而不是使用create方法? 最佳答案 __proto__是非标准的,不会在任何地方都得到支持。Object.create是官方规范的一部分,future的每个环境都应该支持它。它在