我无法弄清楚为什么这个简单的代码不起作用:
index.html的:
<!doctype HTML>
<html>
<head>
<script type="module" src="/showImport.js"></script>
</head>
<body>
<button onclick="showImportedMessage();">Show Message</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
showImport.js:
import showMessage from '/show.js';
function showImportedMessage() {
showMessage();
}
Run Code Online (Sandbox Code Playgroud)
show.js:
export default "Why do I need this?";
export function showMessage() {
alert("Hello!");
}
Run Code Online (Sandbox Code Playgroud)
它由NPM http-server提供服务.当我连接Chrome(v65)时,我看到以下错误
(index):8 Uncaught ReferenceError: showImportedMessage is not defined
at HTMLButtonElement.onclick ((index):8)
onclick @ (index):8
Run Code Online (Sandbox Code Playgroud)
如果我摆脱type=module(并通过将showMessage函数放入导入/导出showImport.js)一切正常,但这样做的全部目的是使用模块.
此外,我不得不添加无用的export default声明,没有它Chrome会抱怨:
Uncaught SyntaxError: The requested module '/show.js' does not provide an export …Run Code Online (Sandbox Code Playgroud) 我有一个上下文菜单,将触发不同的JavaScript函数.选择函数的天真解决方案如下所示:
function(action, el, pos) {
switch(action)
{
case "export_selected_to_excel":
exportSelectedToExcel(el);
break;
etc..
}
}
Run Code Online (Sandbox Code Playgroud)
我希望有一个函数映射,以便我可以将metod减少到与此类似的东西:
function(action, el, pos) {
menuAction[action](el);
}
Run Code Online (Sandbox Code Playgroud)
我像这样定义数组:
function exportSelectedToExcel(id){
//stuff...
}
var menuAction = new Array();
menuAction["export_selected_to_excel"] = exportSelectedToExcel;
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常,感觉像一个合理的解决方案.
在javascript中有这样的缺点吗?
还有更好的方法吗?
例如我有课:
function Test() {
}
Test.prototype = {
'setTest' : function(test) {
this.test = test;
}
}
var test = new Test();
Test.setTest('test');
Run Code Online (Sandbox Code Playgroud)
我想在数据库中保存对象测试.如何将对象测试序列化为字符串?(方法,变量等)
如何使用javascript将类名作为变量传入?
假设我有班级人物.
我想将类的名称传递给一个函数,以便该函数可以调用该类.
所以功能是
function openClass(name)
Run Code Online (Sandbox Code Playgroud)
我想传入
openClass('person')
Run Code Online (Sandbox Code Playgroud)
这样openClass就可以调用类人
例如
function openClass(name)
{
return new name() // here I want this line to actually
// call the class "Person" if that is
// what is passed in as a name parameter,
}
Run Code Online (Sandbox Code Playgroud) 在我的Node.Js应用程序(服务器端)中,我必须创建一个对象实例(这是一个类,所以有new MyClass()),但MyClass是一个string.
可以从String创建对象实例吗?我已经看到在浏览器方面我可以使用window,但在这里我在服务器端......
我将需要这个,因为我现在将在运行时使用该类的名称,因此我无法在"代码"中实例化此对象.
而且,我可以有几个需要以这种方式创建的类.简而言之,我有一个显式这种类的配置文件,我需要在真正的JavaScript对象中转换这个字符串.
我正在为我的应用程序创建动态组件,这些组件可以在程序的各个部分中重复使用。到目前为止,我已经能够创建文本输入,并使用将其动态添加和自定义componentFactory为表单,并且它们可以完美地工作。
下一部分是创建完全动态的按钮,当将其放置在目标视图中时可以对其进行自定义(就像带有表单的文本输入一样)。我试图使大多数事物通用,并且它们可以正常工作,但是我似乎遇到的问题是使(click)函数动态化。我也想添加需要使用触发的功能componentFactory,但是由于某种原因,我无法这样做。
我找不到任何资源可以详细说明我遇到的这个特定问题。这是我到目前为止所做的组件:
button.component.ts
import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';
import { FormGroup } from '@angular/forms';
@Component({
selector: 'app-button',
templateUrl: './button.component.html',
styleUrls: ['./button.component.scss'],
encapsulation: ViewEncapsulation.None
})
export class ButtonComponent implements OnInit {
@Input() group: FormGroup;
@Input() type: string;
@Input() description: string;
@Input() class: string;
@Input() data: string;
@Input() callFunction: string;
constructor() { }
ngOnInit() {
}
}
Run Code Online (Sandbox Code Playgroud)
button.component.html
<div [formGroup]="group">
<button type="{{ type }}" class="{{ class }}" (click)="{{callFunction}}">{{ description }}</button>
</div> …Run Code Online (Sandbox Code Playgroud) 我试图通过将函数名称作为字符串传递然后调用它来访问嵌套函数.例如,请看这篇文章
function outer(action){
window["outer"][action]();
function inner(){
alert("hello");
}
}
outer("inner");
Run Code Online (Sandbox Code Playgroud)
但它不起作用.错误:
window.outer[action] is not a function
Run Code Online (Sandbox Code Playgroud)
如何使这项工作,或调用嵌套函数的替代方法.
这样做的原因是我试图隐藏一系列由函数作用域内的iframe调用的函数.
我有这个代码:
var Functionify = function() {
return {
init: function(el, t) {
var els = document.getElementsByClassName(el);
var elsL = els.length;
while(elsL--){
//els[elsL].onclick = els[elsL].getAttribute(t);
els[elsL].addEventListener('click', els[elsL].getAttribute(t), false);
}
}
};
}();
Run Code Online (Sandbox Code Playgroud)
哪里el = 'myClassName'和t = 'data-id'
现在,'t'是一个字符串,如何告诉addEventListener函数使用't'(一个字符串)作为函数名?
关于按名称动态调用函数有很多类似的问题.但是,我找不到我的特定问题的解决方案,其中我在闭包内部有本地函数,而不将函数暴露给我的对象的公共接口.
让我们看一些代码(这是一个虚构的例子)......
(function(window,$) {
MyObject = (function($) {
var obj = {};
obj.publicMethod = function(number,otherarg) {
this['privateMethod'+number].apply(this,[otherarg]);
};
var privateMethod1 = function(arg) {
//do something with arg
};
var privateMethod2 = function(arg) {
//do something else with arg
};
return obj;
})($);
window.MyObject = MyObject;
})(window,jQuery);
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为"this"是MyObject,并且不公开本地函数.此外,我希望能够在尝试调用之前检查函数是否存在.例如.
var func_name = 'privateMethod'+number;
if($.isFunction(this[func_name])) {
this[func_name].apply(this,[otherarg]);
}
Run Code Online (Sandbox Code Playgroud)
我不确定如何继续进行,除了将我的私有函数暴露给公共接口之外,它一切正常.
obj.privateMethod1 = function(arg) {
//do something with arg
};
obj.privateMethod2 = function(arg) {
//do something else with arg
};
Run Code Online (Sandbox Code Playgroud)
我的想法已经不多了.非常感谢您的帮助和建议.
我试图将字符串转换为控制器中的AngularJS服务方法调用.例如,我想转换字符串"Contact.send(email)"来调用现有的服务方法.我想用:
window["Contact"]["send"](email);
就像在这个线程中一样 - 当我把它的名字作为一个字符串时如何执行JavaScript函数 - 但它表示Contact服务是未定义的,尽管被注入到控制器中.
javascript ×9
node.js ×2
angular ×1
angularjs ×1
closures ×1
ecmascript-6 ×1
import ×1
module ×1
scope ×1
typescript ×1