草庐IT

拷贝构造

全部标签

javascript - 为什么在进行 Javascript 继承时要创建一个临时构造函数?

为什么goog.inherits来自GoogleClosureLibrary看起来像这样:goog.inherits=function(childCtor,parentCtor){functiontempCtor(){};tempCtor.prototype=parentCtor.prototype;childCtor.superClass_=parentCtor.prototype;childCtor.prototype=newtempCtor();childCtor.prototype.constructor=childCtor;};而不是goog.inherits=functio

javascript - "this"在构造函数中分配的函数中如何工作?

我找到了这个示例代码:functionpersonFullName(){returnthis.first+''+this.last;}functionPerson(first,last){this.first=first;this.last=last;this.fullName=personFullName;}vardude=newPerson("Michael","Jackson");alert(dude.fullName());这会提醒“MichaelJackson”。我将其更改为从构造函数调用personFullName而不是分配函数对象:functionpersonFullNa

函数调用/构造函数调用前带有 [...] 的 Javascript 语法

我在javascript中发现了一种我从未发现过的语法以前见过,但我找不到合适的文档。它来自一个教程:varconnection=new[webkit|moz]RTCPeerConnection(...)webkit是什么意思,moz是什么意思,我自己能找到,大概这是两个定义的常量或枚举。但我的问题是:方括号中的语法[webkit|moz]是什么意思?是否对函数结果进行了某种类型转换?|字符在[webkit|moz]中是什么意思-这是OR运算符吗?谢谢 最佳答案 这不是正确的javascript语法(如果你尝试运行它,你会在第一个[

javascript - Java 8 Lambda 构造和 JavaScript 之间的确切区别是什么?

关闭。这个问题需要更多focused.它目前不接受答案。想改进这个问题吗?更新问题,使其只关注一个问题editingthispost.关闭6年前。Improvethisquestion我必须将Java8代码转换为JavaScript(一种方式,一生一次)。为了加快速度,我想尽可能地自动化,然后使用测试套件来解决所有遗留问题。我想知道Java8lambda和JavaScript(函数)之间的区别是什么?任何重要的不兼容性?

javascript - 何时/为什么在构造函数上使用 JavaScript 中的类?

这个问题在这里已经有了答案:WhatbenefitsdoesES2015(ES6)`class`syntaxprovide?(2个答案)关闭5年前。是的,有很多方法可以创建和使用对象。那么为什么/什么时候创建构造函数比声明一个类并使用constructor()方法更好呢?我的导师说这没有什么区别,但我不相信他。//1functionGrumpy(name,profile,power){this.name=name;this.profile=profile;this.power=power;}对比//2classGrumpy{constructor(name,profile,power)

javascript - “var” 变量、"this"变量和 "global"变量 - 在 JavaScript 构造函数中

在我上一个问题之后,这个问题对我来说更准确:例子:functionFoo(){this.bla=1;varblabla=10;blablabla=100;this.getblabla=function(){returnblabla;//exposesblablaoutside}}foo=newFoo();我现在的理解:this.bla=1;//willbecomeanattributeofeveryinstanceofFOO.varblabla=10;//willbecomealocalvariableofFoo(will**not**becomeanattributeofeveryi

javascript - 为什么构造函数不是构造函数?

考虑以下片段:​f=function(){};f.prototype={};thing=newf;我惊讶地发现thing.constructor是Object()。(参见fiddlehere。)为什么thing.constructor不是函数f? 最佳答案 因为您已经将f的原始prototype对象完全替换为普通对象。它是原始的prototype对象,它通过.constructor属性保存对f的引用。使用对象字面量语法创建的对象的构造函数将是Object构造函数。要取回它,您需要手动将其放在那里。f=function(){};f.p

javascript - 在构造函数中调用异步函数。

getUser是一个异步函数?如果需要更长的时间来解决?它是否总是会在我的someotherclass中返回正确的值。classIdpServer{constructor(){this._settings={//someidentityserversettings.};this.userManager=newUserManager(this._settings);this.getUser();}asyncgetUser(){this.user=awaitthis.userManager.getUser();}isLoggedIn(){returnthis.user!=null&&!th

javascript - 自推对象构造器

所以JavaScript是一种函数式语言,类是由函数定义的,函数作用域对应于类的构造函数。经过相当长的时间研究如何在JavaScript中进行OOP,我现在明白了。我想做的事情不一定可行,所以我首先想知道这是否是个好主意。假设我有一个数组和一个如下所示的类:varEntry=function(name,numbers,address){this.name=name;if(typeofnumbers=="string"){this.numbers=[];this.numbers.push(numbers);}elsethis.numbers=numbers;this.address=ad

javascript - 浅拷贝对象遗漏了 ES6/ES7 中的一个或多个属性?

这就是我一直在做的:varprops={id:1,name:'test',children:[]}//copypropsbutleavechildrenoutvarnewProps={...props}deletenewProps.childrenconsole.log(newProps)//{id:1,name:'test'}有没有更干净、更简单的方法? 最佳答案 你可以使用destructuringassignment:varprops={id:1,name:'test',children:[]}var{children:_,.