我是iPhone应用程序编程的初学者.
我真的不喜欢我们设置起源和尺寸的方式,如:
UIView *view;
CGRect frame = view.frame;
frame.origin.x = 100;
view.frame = frame;
Run Code Online (Sandbox Code Playgroud)
要么:
UIView *view;
view.frame = CGRectMake(100, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
Run Code Online (Sandbox Code Playgroud)
所以我为UIView创建了一个类别:
@interface UIView (Origin)
-(void) setOriginX:(CGFloat)x;
-(void) setOriginY:(CGFloat)y;
-(void) setOriginX:(CGFloat)x y:(CGFloat)y;
-(void) setWidth:(CGFloat)w;
-(void) setHeight:(CGFloat)h;
-(void) setWidth:(CGFloat)w height:(CGFloat)h;
@end
@implementation UIView(Origin)
-(void) setOriginX:(CGFloat)x {
self.frame = CGRectMake(x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
}
...
@end
Run Code Online (Sandbox Code Playgroud)
然后我可以写:
UIView *view;
[view setOriginX 100];
Run Code Online (Sandbox Code Playgroud)
这对我来说很方便,但是有什么顾虑我不应该做这样的事情,或者直接设置起源/尺寸的任何更简单的方法吗?
我搜索了一下SO,并没有找到任何帮助我的问题/答案.问题是我的jQuery函数调用变得太大而无法维护.我想知道我是否应该重构更多,或者是否有更好的方法来完成所有这些调用.你会看到,当我进行一次调用时,作为函数参数的匿名函数最终会非常大,并使代码的可读性变得非常糟糕.理论上,我可以把所有这些都分解成自己的功能,但我不知道这是不是最好的做法.以下是目前为止jQuery的一些示例:
$('#dialog').html('A TON OF HTML TO BUILD A FORM').dialog('option', 'buttons', { 'Save': function(){$.post('/use/add/', $('#use_form').serialize(), function(data){ ......There were 4 more lines of this but I'm saving you so you don't rip your eyeballs out hopefully you get the idea.....dialog('option','title','New Use').dialog('open');
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,因为我正在调用的许多函数都将函数作为参数,当我创建匿名函数时,我最终得到了一个巨大的混乱(此代码中还有大约3个匿名函数声明)
我应该简单地制作一堆函数并调用它们以使其更具可读性.我反对这个的唯一原因是因为我声明了一堆只使用过一次的函数.
在此先感谢您的帮助!
我正在研究一个简单的问题来表示一些具有层次结构的类型.有一个数据行包含一些数据,数据可能因行的类型类型而异.简单行可能只有标题和日期,而扩展行可能包含标题,描述,日期和图像.我不是Javascript的新手,但不太了解它继续前进.举一个简单的例子,下面是我将如何用Java编写它:
interface Row {
View getView();
}
class BasicRow implements Row {
private String title;
private String description;
public BasicRow(String title, String description) {
this.title = title;
this.description = description;
}
public View getView() {
// return a View object with title and description
}
}
class ExtendedRow implements Row {
private String title;
private String description;
private Date date;
private Image image;
public ExtendedRow(String title, String description, Date date, Image image) {
this.title = title;
this.description = description;
this.date …Run Code Online (Sandbox Code Playgroud) 我有这个JavaScript代码:
for (var idx in data) {
var row = $("<tr></tr>");
row.click(function() { alert(idx); });
table.append(row);
}
Run Code Online (Sandbox Code Playgroud)
所以我正在查看一个数组,动态创建行(我创建单元格的部分被省略,因为它并不重要).重要的是我创建了一个包含idx变量的新函数.
但是,idx只是一个引用,因此在循环结束时,所有行都具有相同的功能,并且所有行都提醒相同的值.
我现在解决这个问题的一种方法是这样做:
function GetRowClickFunction(idx){
return function() { alert(idx); }
}
Run Code Online (Sandbox Code Playgroud)
在我调用的调用代码中
row.click(GetRowClickFunction(idx));
Run Code Online (Sandbox Code Playgroud)
这有效,但有点难看.我想知道是否有更好的方法可以在循环中复制idx的当前值?
虽然问题本身不是特定于jQuery(它与JavaScript闭包/范围有关),但我使用jQuery,因此只有jQuery的解决方案才有效.
在Javascript中,我们可以直接调用字符串文字的方法而不将其括在圆括号内.但不适用于其他类型,如数字或函数.这是一个语法错误,但有没有理由为什么Javascript词法分析器需要将这些其他类型括在圆括号中?
例如,如果我们使用alert方法扩展Number,String和Function并尝试在文字上调用此方法,则它是Number和Function的SyntaxError,而它适用于String.
function alertValue() {
alert(this);
}
Number.prototype.alert = alertValue;
String.prototype.alert = alertValue;
Function.prototype.alert = alertValue;
Run Code Online (Sandbox Code Playgroud)
我们可以直接在字符串对象上调用alert:
"someStringLiteral".alert() // alerts someStringLiteral
Run Code Online (Sandbox Code Playgroud)
但它是关于数字和函数的SyntaxError.
7.alert();
function() {}.alert();
Run Code Online (Sandbox Code Playgroud)
要使用这些类型,我们必须将其括在括号内:
(7).alert(); // alerts "7"
(function() {}).alert(); // alerts "function() {}"
Run Code Online (Sandbox Code Playgroud)
更新:
@Crescent的链接和@Dav和@Timothy的答案解释了为什么7.alert()失败,因为它正在寻找一个数值常量,并且为了超越它,插入额外的空格或额外的点.
7 .alert()
7..alert()
7. .alert();
Run Code Online (Sandbox Code Playgroud)
是否有类似的语法原因,为什么在调用方法之前需要将函数括在括号中?
我不熟悉解释器和词法分析器,知道它是否可以通过某种先行的方式解决,因为Ruby是一种动态语言并且可以解决这个问题.例如:-
7.times { |i| print i }
Run Code Online (Sandbox Code Playgroud)
更新2:
@ CMS的答案一直在理解为什么功能不起作用.以下陈述有效:
// comma operator forces evaluation of the function
// alerts "function() {}"
<any literal>, function() {}.alert();???????
// all examples below are forced …Run Code Online (Sandbox Code Playgroud) 亲爱的专家,我试图用JS动态生成DOM元素.
我从Douglas Crockford的书中读到,DOM的结构非常糟糕.
无论如何,我想创建一些DIVISION元素并将引用存储到一个数组中,以便以后可以访问它.
这是代码
for(i=0; i<3; i++) {
var div = document.body.appendChild(document.createElement("div"));
var arr = new Array();
arr.push(div);
}
Run Code Online (Sandbox Code Playgroud)
不知何故,这将无法工作.....只创建了1个div元素.当我使用它arr.length来测试代码时,数组中只有1个元素.
还有另一种方法来实现这一目标吗?
提前致谢
我试图从俄罗斯航运网站获取一些信息.作为JON/Jquery/Internets的n00b,我无法将数据转换为json格式.
按照公司的API,我转到URL:http://emspost.ru/api/rest/?callback = json&method = theems.calculate&from = city--abakan& to = city--anadyr&weight = 1
返回:
json({"rsp":{"stat":"ok","price":"750","term":{"min":5,"max":9}}})
Run Code Online (Sandbox Code Playgroud)
在Jquery的文档之后,我尝试过:
<script>$.getJSON("http://emspost.ru/api/rest/?callback=json&method=ems.calculate&from=city--abakan&to=city--anadyr&weight=1",
function(data){
alert(data);
});</script>
Run Code Online (Sandbox Code Playgroud)
返回null.知道我做错了什么吗?
So I have some javascript class and in one method I use jQuery to bind function to click event. And within this function I need to call other methods of this class. In usual js function I did it through "this.method_name()", but here, I guess, jQuery redefines "this" pointer.
我现在试着让它工作几个小时,但却无法做到正确.
我有以下代码:
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSArray arrayWithObjects:@"currentGame1", @"currentGameType1", @"currentGameQuestion1", @"currentGameRightAnswers1", @"currentGameType1", @"numberOfType0Games1", @"type0Results1", @"numberOfType1Games1", @"type1Results1",@"numberOfType2Games1", @"type2Results1",nil], @"Player1",
[NSArray arrayWithObjects:@"currentGame2", @"currentGameType2", @"currentGameQuestion2", @"currentGameRightAnswers2", @"currentGameType2", @"numberOfType0Games2", @"type0Results2", @"numberOfType1Games2", @"type1Results2",@"numberOfType2Games2", @"type2Results2",nil], @"Player2",
[NSArray arrayWithObjects:@"currentGame3", @"currentGameType3", @"currentGameQuestion3", @"currentGameRightAnswers3", @"currentGameType3", @"numberOfType0Games3", @"type0Results3", @"numberOfType1Games3", @"type1Results3",@"numberOfType2Games3", @"type2Results3",nil], @"Player3",nil];
[dict writeToFile:@"/Users/MikaelB/Desktop/xxxxPlayer.plist" atomically: TRUE];
NSMutableDictionary *readDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/Users/MikaelB/Desktop/xxxxPlayer.plist"];
NSLog(@"readDict: %@", readDict);
NSLog(@"= = = = = = = = = = = = = = = = = = =");
for (NSArray *key in [readDict …Run Code Online (Sandbox Code Playgroud) 我有一个发出AJAX调用的函数(通过jQuery).在本complete节中,我有一个函数说:
complete: function(XMLHttpRequest, textStatus)
{
if(textStatus == "success")
{
return(true);
}
else
{
return(false);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我这样称呼它:
if(callajax())
{
// Do something
}
else
{
// Something else
}
Run Code Online (Sandbox Code Playgroud)
第一个从未被调用过.
如果我alert(textStatus)在complete函数中放入一个我得到的,但在该函数返回之前不会undefined.
是否可以将回调函数传递给我的callajax()方法?喜欢:
callajax(function(){// success}, function(){// error}, function(){// complete});
javascript ×6
jquery ×5
iphone ×2
ajax ×1
api ×1
callback ×1
categories ×1
closures ×1
dom ×1
inheritance ×1
interface ×1
ios ×1
json ×1
lexer ×1
loops ×1
methods ×1
nsdictionary ×1
objective-c ×1
oop ×1
prototype ×1
refactoring ×1
return-value ×1
syntax ×1
uiview ×1