我想知道是否需要在构造函数后添加分号.看起来像添加分号或不添加,两者都有效.function test() { };function test2() {}解决方法:简而言之,您不需要添加分号. 根据一篇非常详尽地介绍semi-colon necessity in JavaScript主题的文章,这些都是没有分号的行的有效例子:var a=1 var b=2 var c=3// before if(condition) stuff()// after if(condition){stuff() }// after minification if(condition){stuff()}另一方面,这不起作用:a ...
这个问题更多地针对那些专业的开发人员,或者作为自由职业者/团队/企业/等工作. 使用文字javascript符号比构造函数符号更受追捧?编写Javascript时使用的是什么样的符号真的很重要吗?雇主是否关心,还是有更专业的记谱法? 文字说明var snoopy = {species: "beagle",age: 10 };构造函数符号var buddy = new Object(); buddy.species = "golden retriever"; buddy.age = 5;解决方法:如果文字符号适用于您的情况,那么它通常更紧凑,通常...
我正在尝试确定一种创建工厂函数的方法,这样我就可以将类传递给它并接收一个将创建该类实例的函数.类似的东西:function createClassFactory<T> (MyClass: {new(...):T}) {return function classFactory(...) {return new MyClass(...)} }为了维护类型信息,我希望classFactory具有与MyClass构造函数相同的签名.现在,我正在谈论如何做到这一点. 这种事情是否可能以其他方式存在?解决方法:除非你愿意接受所有构造函数参数作为单个对...
我正在编写一些帮助类来编写Adobe Illustrator脚本. 我的问题始于Eyelet对象.当我实例化它时,它在第一个新的Group()失败,因为显然Group没有构造函数. 这是我的代码的精简版:/****************** Collection Class **********************/function Collection() {this.parent = app.activeDocument;this.typename = "Collection"; }Collection.prototype.setName = function(name) {this.instance.name = name;};/***************...
在构造函数中使用自执行anon函数时,我有一个关于上下文/范围的快速问题. 请注意以下工作代码:function Foo() {this.height = 10;this.width = 10;this.init = function() {this.create();};this.create = function() {alert('test');}; } var span1 = new Foo(); span1.init();警报显示为预测.但是,我不想在底部调用span1.init.我宁愿让Constructor函数中的init函数自动执行.这将给我如下代码:function Foo() {this.height = 10;...
我试图验证一个咖啡脚本构造函数抛出一个异常(使用sinon和sinon-chai).class Animalconstructor: ->throw "exception"class Dog extends Animal如何正确创建间谍以验证实例化Dog抛出异常?解决方法:只需使用匿名函数在里面创建对象,就可以将期望值传递给它.到目前为止还没有需要的僧expect(-> new Animal()).to.throw(/message/)请注意,如果你抛出一个简单的字符串,那么chai’s throw中的正则表达式总会通过.它只有在抛出Error对象...
<script> function User (theName, theEmail) {this.name = theName;this.email = theEmail; }User.prototype = {constructor: User,changeEmail:function (newEmail) {this.email = newEmail;return "New Email Saved: " + this.email;} } // A User firstUser = new User("Richard", "Richard@examnple.com"); firstUser.changeEmail("RichardB@examnple.com"); </script>这段代码取自这里:http://javascriptissexy.com/oop-...
function Human(){this.job = 'code' }//Human.prototype = {feeds: 'Pizza'};var developer = new Human();console.log(developer.constructor);控制台日志上方function Human() { this.job = 'code'; }当我取消注释Human.prototype = {feeds:’Pizza’}时;它控制台日志function Object() {[native code] }为什么在构造函数上设置原型,会影响谁是构造函数创建的对象的构造函数? 另一个例子:function LivingBeing() {breathes: ...
我正在学习React 16.3,它是新的Context API.特别是Updating Context from a Nested Component.在他们的示例中,他们设置了一个在构造函数中定义的方法,而不是标准方法.class App extends React.Component {constructor(props) {super(props);// What is the benefit of doing this here?this.toggleTheme = () => {this.setState(state => ({theme:state.theme === themes.dark? themes.light: themes.dark,}));};this.state = {the...
因为不可能使用带有参数数组的新运算符(.call或.apply),所以我想知道我是否可以手动完成. 在下面的代码中,obj1和obj2等价(在ES3中)?function MyClass(a, b, c) {}var obj1 = new MyClass(2,3,4)var tmp = function() {} tmp.prototype = MyClass.prototype; var obj2 = new tmp; obj2.constructor = MyClass; MyClass.call(obj2, 2,3,4);– edit1 – 您的答案是否意味着上述代码错误或不等同于对象构造?解决方法:使用以下语法...
我对在构造函数中声明函数的不同方法感到困惑.function ClassName() {this.one = function() {};var two = function() {}; three = function() {}; }我知道一个是公开的,可以被外面打电话,两个是私人的.三个语义是什么?解决方法:您提供的示例将是语法错误,因为您需要在该上下文中使用=进行赋值. 如果与正确的赋值运算符一起使用,则三个将是一个存在于该范围之外的全局函数.省略var关键字时,会为变量分配全局对象的属性,该属性是浏...
我正在使用introjs库. 请参阅原始代码here. 我希望能够编写var = new IntroJs()而不是调用start()方法. 我怎样才能做到这一点?解决方法:为什么不简单地包装introJs提供的工厂并在包装器中调用它? 您可以使用此类内容(未经测试)在外部执行此操作:var introJsWrapper = function(targetElm) {var ijs = introJs(targetElm);ijs.start();return ijs; };或者你可以通过将它作为主函数的属性公开,在introJs代码的一个fork中做到这一点...
在MDN JavaScript指南中,在“Property inheritance revisited”部分下,它解释了属性继承的一些细微之处. 我特别感兴趣的是一个关于递增id的例子:var idCounter = 1;function Employee (name, dept) { this.name = name || ""; this.dept = dept || "general"; this.id = idCounter++; }function Manager (name, dept, reports) {...} Manager.prototype = new Employee;function WorkerBee (name, dept, projs) {...} WorkerBee.p...
使用新的Function(params,body)构造函数从JavaScript代码创建新函数时,在body yelds SyntaxError中传递无效字符串.虽然此异常包含错误消息(即:Unexpected token =),但似乎不包含上下文(即行/列或发现错误的字符). 示例小提琴:https://jsfiddle.net/gheh1m8p/var testWithSyntaxError = "{\n\n\n=2;}";try {var f=new Function('',testWithSyntaxError); } catch(e) {console.log(e instanceof SyntaxError); console.log(e.messa...
当我做:var person = new Object(); person.name = "alex"; console.log(person)输出是:Object { name="alex"}但是,我说放弃“新”字并做:var person = Object(); person.name = "alex"; console.log(person)输出还是:Object { name="alex"}为什么?解决方法:因为一些内置函数只是定义为以这种方式运行.例如,见ES5 15.2.1.1 for Object:07001 When the Object function is called with no arguments or with one argument...