草庐IT

Object.prototype.toString判断类型的原理

全部标签

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 - 如何在 jQuery 中检测当前页面的媒体类型

我有一个带有输入字段的页面。当页面处于媒体类型打印时,我想隐藏所有0值文本字段。我试过jQuery.但这在屏幕模式和打印模式下都有效。HTML:somescreensomeprintPrintJS:$('button').click(function(){$('input[type=text]').each(function(){if($(this).val()==0){$(this).hide()}})})CSS:@mediaprint{.screen{display:none;}.print{display:block;}}@mediascreen{.screen{display:

javascript - JavaScript 中的 'Worker' 是什么类型的对象

我对这一切有点困惑......Chrome和Firefox都告诉我不同​​的事情,我在规范中找不到提到它的任何部分,但是:在Chrome中:ObjectinstanceofFunction//trueFunctioninstanceofObject//trueWorkerinstanceofObject//trueWorkerinstanceofFunction//false在FireFox中:ObjectinstanceofFunction//trueFunctioninstanceofObject//trueWorkerinstanceofObject//falseWorkerin

javascript - 使用 Object.create 而不是 new 时传递参数

这个问题不是Using"Object.create"insteadof"new"的重复问题.有问题的线程在使用Object.create时没有专注于正确传递参数我很好奇如何使用Object.create而不是new来初始化对象。到目前为止,这是我的代码:functionHuman(eyes){this.eyes=eyes||false;}Human.prototype.hasEyes=function(){returnthis.eyes;}functionMale(name){this.name=name||"Noname";}Male.prototype=newHuman(true)

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的每个环境都应该支持它。它在

javascript - JS 原型(prototype)继承 : childs use the same parent properties?

假设我有Player对象:varplayer=function(name){this.handlers={};}player.prototype.on=function(event,callback){if(!this.handlers[event]){this.handlers[event]=[];}this.handlers[event].push(callback);}效果很好,我可以创建播放器,每个播放器都有自己的一组处理程序。现在假设我需要从player继承:vartestPlayer=function(name){this.name=name;};testPlayer.pr

javascript - 明确的 ".prototype"真的需要吗?

我经常在别人的脚本中看到这样的东西:bar=Array.prototype.slice.call(whatever,1)但是,以下较短的符号也可以正常工作:bar=[].slice.call(whatever,1)这两个结构是否完全等价?是否存在以不同方式对待它们的引擎(浏览器)? 最佳答案 是的,完全等同。碰巧通​​过.prototype的访问稍微快一些,因为不需要创建新的对象实例。然而,这就是我们所说的微优化。完全摆脱深度链接的一个好方法是调用Function.prototype.bind。例子(function(slice){

javascript - 为什么在扩展对象时使用 Object.create 作为原型(prototype)?

我有Java背景,最近一直在尝试JavaScript继承。我开始编写一些对象,在阅读了一些示例后,我找到了最适合我的代码风格。这是我的:varClass=function(){};Class.extend=function(p_constructor){varSuperclass=this;//thefollowinglineconfusesmep_constructor.prototype=Object.create(Superclass.prototype);p_constructor.prototype.constructor=p_constructor;p_constructo