我正在使用使用原型扩展的基本自定义对象
function Person() {}
Person.prototype.Name = "";
Person.prototype.Lastname = "";
var NewPerson= new Person();
NewPerson.Name = "Nancy";
NewPerson.Lastname = "Drew"; //<--- right way
NewPerson.lastname = "Drew"; //<--- wrong property
Run Code Online (Sandbox Code Playgroud)
我需要避免在定义的对象中添加新的属性和方法,因为它会产生无声的错误和错误。
我知道 javascript 有一种糟糕的方式来管理类/对象,但是有办法保护它吗?
我发现冻结和密封,但这些阻止我更改值。
我一直在寻找一种方法来永久更改 HTMLFormElement Javascript 对象的“onsubmit”行为。假设我的 JavaScript 之一包含以下代码:
HTMLFormElement.prototype.onsubmit = function () {
this.submit();
// Make sure that you can only submit once.
this.onsubmit = function () {
return false;
};
return false;
};
Run Code Online (Sandbox Code Playgroud)
这个想法只是为了防止所有形式的文档的重复提交。由于上面的示例不起作用,我无法弄清楚如何执行此操作。
有没有办法在文档加载后无需循环遍历所有现有 HTMLElement 来实现此目的?这是一种与某些较旧的浏览器兼容的方法,并且如果您使用其他脚本创建新的表单元素,也可以保留该行为。(仅限香草 JS)
我正在尝试使用 Jint (v2.10.4.0) 将一个任意 JSON 结构转换为另一种结构。但是,我在使用map时遇到问题。
根据 ECMA 5.1 语言规范,map应该存在于 Array.prototye 上。但是,当我尝试使用它时,出现错误:Jint.Runtime.JavaScriptException: 'Object has no method 'map''
我正在测试这个
Engine engine = new Engine();
var doubles = engine.SetValue("x", "[ 1, 2, 3, 4, 5 ]")
.Execute("x.map(function(a){ return a + a; })")
.GetCompletionValue()
.ToObject();
Console.WriteLine(doubles);
Console.ReadKey();
Run Code Online (Sandbox Code Playgroud)
理想情况下,我还想使用 find,尽管这是 ECMA6。我在使用 Array.Prototype.map 时缺少什么吗?或者有没有办法为 Jint 引入 polyfill?
我有两个屏幕。主页和照片详细信息。在主页上我有照片列表。如果我喜欢照片详细信息屏幕上的照片,我想更新主屏幕上的喜欢。无需更新整个页面。我可以在屏幕之间使用回调吗?如何?我正在使用 StackNavigator。谢谢你的回答
<TouchableHighlight
onPress={() => navigate('PhotoDetails', {
id: rowData.caption.id,
tag: rowData.caption.text,
url: rowData.images.standard_resolution.url,
likes: this.state.likes[rowData.caption.id] || 0
})}>
<Image
source={{uri:rowData.images.standard_resolution.url}}
style={{width: 320, height: 320}}/>
</TouchableHighlight>Run Code Online (Sandbox Code Playgroud)
<Like
onPress={()=> {onLike(this.state.id).then((newLikes)=>{
this.setState({
likes: newLikes
})
})}}
likes={this.state.likes + '' || '0'}
/>Run Code Online (Sandbox Code Playgroud)
我定义了一个函数如下:
function getCurrentComponent(){
if($rootRouter._currentInstruction){
return $rootRouter.recognize($rootRouter._currentInstruction.urlPath).then(function (data) {
return data.component.componentType;
});
}else{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
为了调用这个函数,我做了如下操作:
factory.getCurrentComponent().then(function (data) {
...
});
Run Code Online (Sandbox Code Playgroud)
问题是当getCurrentComponent函数返回空值时,会生成以下错误:
无法读取 null 的属性“then”
我该如何解决这个问题?
我忘了说我仅限于使用 ES5,所以我无法使用该对象Promise
在将大量文件转换为typescript时,我有许多以这种方式声明的类.
function FooClass() {
this.bar = 1; // error TS2683: 'this' implicitly has type 'any'
// because it does not have a type annotation.
}
FooClass.prototype.myMethod = function() {
// ...
}
Run Code Online (Sandbox Code Playgroud)
如何在打开严格类型检查的情况下完成此工作,同时避免使用类语法重写所有内容?
我有以下进口:
import { default as service } from "../service";
Run Code Online (Sandbox Code Playgroud)
VS
import * as service from "../service";
Run Code Online (Sandbox Code Playgroud)
我的服务是这样导出的
module.exports = {
init(store) {
_store = store;
},
beginPayment() {
}
};
Run Code Online (Sandbox Code Playgroud)
我希望只有第二次导入会起作用,因为没有导出默认值,但是两者似乎都有效。
这些有什么区别?一个比另一个更受欢迎吗?
如果这是重复的,我道歉,我在 SO 或 Google 上没有找到任何特定于我的示例的内容。
我有一个看起来像这样的对象数组
const data = [
{id: 1, locale: 'en'},
{id: 2, locale: 'nl'}
]
Run Code Online (Sandbox Code Playgroud)
现在,我试图过滤掉数组中每个项目的locale属性(不要永久删除它,只需将其过滤掉一次),因此我的数据在理想情况下类似于此:
const data = [
{id: 1},
{id: 2}
]
Run Code Online (Sandbox Code Playgroud)
使用map函数来散布属性,但是我对如何继续此操作感到困惑。
this.translations.map(translation => {
return { ...translation }
})
Run Code Online (Sandbox Code Playgroud)我将以下代码用于带有Siema的滑块:
https://codepen.io/pawelgrzybek/pen/boQQWy
它使用扩展类向幻灯片添加点。一切正常,除了我们的网站现在在使用 ES6 进行 Google 移动友好测试时遇到问题,因为它给出了错误:
Uncaught SyntaxError: Unexpected reserved word
Run Code Online (Sandbox Code Playgroud)
在这一行:
class SiemaWithDots extends Siema {
Run Code Online (Sandbox Code Playgroud)
有没有办法让它与 ES5 兼容?
代码如下:
// instantiate new extended Siema
const mySiemaWithDots = new SiemaWithDots({
// on init trigger method created above
onInit: function(){
this.addDots();
this.updateDots();
},
// on change trigger method created above
onChange: function(){
this.updateDots()
},
});
// extend a Siema class by two methods
// addDots - to create a markup for dots
// updateDots - to update …Run Code Online (Sandbox Code Playgroud) 我想将 Typescript 文件编译为多个目标,例如 ES5 和 ES6。我有以下示例目录:
ES5
ES6
测试文件
配置文件
因此,当我运行编译器时,我希望它将 test.ts 编译为 ES5 到 ES5 文件夹,将 ES6 编译为 ES6 文件夹。这有可能吗?
ecmascript-5 ×10
javascript ×7
ecmascript-6 ×5
typescript ×2
angularjs ×1
c# ×1
class ×1
dom ×1
extend ×1
html ×1
import ×1
jint ×1
react-native ×1
tsconfig ×1