class PlayerControls extends React.Component {
constructor(props) {
super(props)
this.state = {
loopActive: false,
shuffleActive: false,
}
}
render() {
var shuffleClassName = this.state.toggleActive ? "player-control-icon active" : "player-control-icon"
return (
<div className="player-controls">
<FontAwesome
className="player-control-icon"
name='refresh'
onClick={this.onToggleLoop}
spin={this.state.loopActive}
/>
<FontAwesome
className={shuffleClassName}
name='random'
onClick={this.onToggleShuffle}
/>
</div>
);
}
onToggleLoop(event) {
// "this is undefined??" <--- here
this.setState({loopActive: !this.state.loopActive})
this.props.onToggleLoop()
}
Run Code Online (Sandbox Code Playgroud)
我想loopActive
在切换时更新状态,但是this
在处理程序中未定义对象.根据教程文档,我this
应该参考该组件.我错过了什么吗?
有两种方法可以引用该类中的类实例.例如:
class Person {
String name;
public void setName(String name) {
this.name = name;
}
public void setName2(String name) {
Person.this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
一个用于this.name
引用对象字段,但另一个用于className.this
引用对象字段.这两个引用有什么区别?
我是新来的Android和我想明白之间的差别getApplication()
,getApplicationContext(
)getBaseContext()
,getContext()
以及someClass.this
特别是当使用这些方法在下面的代码行:
当我发起祝酒时,这些和我使用它们之间有什么区别?
Toast.makeText(LoginActivity.this, "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplication(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), "LogIn successful", Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)
与意图相同:
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
Intent intent = new Intent(MenuPagina., LoginActivity.class);
Intent intent = new Intent(getBaseContext(), LoginActivity.class);
Intent intent = new Intent(getApplication(), LoginActivity.class);
Run Code Online (Sandbox Code Playgroud) 我正在进行Android编程并且正在学习Intents,当我看到一个构造函数时,对我的C#训练有素的思想,看起来很时髦.电话是:
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
Run Code Online (Sandbox Code Playgroud)
这两个参数对我来说都是新的.如何从班级名称中删除静态".this"?这是Java的东西还是Android的东西?我假设它只是说"this",因为我在上下文中CurrentActivity
,但我不知道如何从类名本身调用"this".也.".class"看起来像是用于反射,我在C#中很熟悉,但对此的任何见解也会受到欢迎.
谢谢.
在一个类方法中看到这一行,我的第一反应是嘲笑编写它的开发人员..但是,我认为我应该确保我是对的.
public void dataViewActivated(DataViewEvent e) {
if (this != null)
// Do some work
}
Run Code Online (Sandbox Code Playgroud)
那条线路会被评估为假吗?
我有一个看起来像这样的Java程序.
public class LocalScreen {
public void onMake() {
aFuncCall(LocalScreen.this, oneString, twoString);
}
}
Run Code Online (Sandbox Code Playgroud)
LocalScreen.this
意味着什么aFuncCall
?
我不确定在TypeScript中处理"this"范围的最佳方法.
这是我转换为TypeScript的代码中常见模式的示例:
class DemonstrateScopingProblems {
private status = "blah";
public run() {
alert(this.status);
}
}
var thisTest = new DemonstrateScopingProblems();
// works as expected, displays "blah":
thisTest.run();
// doesn't work; this is scoped to be the document so this.status is undefined:
$(document).ready(thisTest.run);
Run Code Online (Sandbox Code Playgroud)
现在,我可以将呼叫改为......
$(document).ready(thisTest.run.bind(thisTest));
Run Code Online (Sandbox Code Playgroud)
......确实有效.但它有点可怕.这意味着代码可以在某些情况下编译和工作正常,但如果我们忘记绑定范围,它将会中断.
我想在类中做一个方法,这样在使用类时我们不需要担心"this"的作用范围.
有什么建议?
另一种方法是使用胖箭头:
class DemonstrateScopingProblems {
private status = "blah";
public run = () => {
alert(this.status);
}
}
Run Code Online (Sandbox Code Playgroud)
这是一种有效的方法吗?
我一直$this
在PHP中看到变量,我不知道它用于什么.我从来没有亲自使用它,搜索引擎忽略了这一点$this
,我最终搜索了"this"这个词.
有人能告诉我变量$这在PHP中是如何工作的吗?
高级标题,简单问题:
如何在jQuery中执行以下操作(隐藏除了之外的所有内容$(this)
)?
$("table tr").click(function() {
$("table tr:not(" + $(this) + ")").hide();
// $(this) is only to illustrate my problem
$("table tr").show();
});
Run Code Online (Sandbox Code Playgroud) 我正在使用Reactjs编写一个菜单组件.
"use strict";
var React = require("react");
var Menus = React.createClass({
item_url: function (item,categories,articles) {
console.log('afdasfasfasdfasdf');
var url='XXX';
if (item.type == 1) {
url = item.categoryId == null ? 'javascript:void(0)' : path('buex_portal_browse_category', {slug: categories[item.categoryId].slug});
} else if (item.type == 2) {
url = item.articleId == null ? 'javascript:void(0)' : path('buex_portal_view_article', {slug: articles[item.articleId].slug, id: item.articleId});
} else {
url = item.url;
}
return url;
},
render: function () {
// console.log(this.props.menus); // return correctly
var menuElements = this.props.menus.map(function (item1) { // …
Run Code Online (Sandbox Code Playgroud) this ×10
java ×4
android ×2
class ×2
reactjs ×2
javascript ×1
jquery ×1
map-function ×1
oop ×1
php ×1
toast ×1
typescript ×1