我是javascript的新手,我想知道为什么bob.age
仍然是30而age
是50,当我打电话给它时。我已将age
设置为setAge
中的50,将{50}分配给this.age
,我知道这是对bob.age
的引用,因此age
和{{1}应该具有相同的值。
bob.age
答案 0 :(得分:7)
你的代码/理解中有几个错误,我将首先解释为什么这是错误的,并解释它周围的概念
in viewWillAppear
//xmpp user array
self.xmppUserArray=[[[self fetchedResultsController] fetchedObjects] mutableCopy];
for (int i=0; i<[[self xmppUserArray] count]; i++) {
if ([[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"sectionNum"] integerValue]==0) {
//this is user is online
[[[AKSGetCareerGlobalClass SharedInstance] onlineUserArray] addObject:[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"nickname"]];
}
}
//also implement method
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
//remove previous data or clear array
[[self xmppUserArray] removeAllObjects];
[[[AKSGetCareerGlobalClass SharedInstance] onlineUserArray] removeAllObjects];
//get data from core data
self.xmppUserArray=[[[self fetchedResultsController] fetchedObjects] mutableCopy];
for (int i=0; i<[[self xmppUserArray] count]; i++) {
if ([[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"sectionNum"] integerValue]==0) {
//this is user is online
[[[AKSGetCareerGlobalClass SharedInstance] onlineUserArray] addObject:[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"nickname"]];
}
}
[[self msgTableView] reloadData];
}
-(NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController == nil)
{
NSManagedObjectContext *moc = [[self appDelegate] managedObjectContext_roster];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject"
inManagedObjectContext:moc];
NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil];
//NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];
//NSString *myJID = [[NSUserDefaults standardUserDefaults] stringForKey:@"userJID"];
//NSLog(@"My JID ====>%@",myJID);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subscription=='both'"];//take care about subscription
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchBatchSize:20];
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:moc
sectionNameKeyPath:@"sectionNum"
cacheName:nil];
[fetchedResultsController setDelegate:self];
NSError *error = nil;
if (![fetchedResultsController performFetch:&error])
{
DDLogError(@"Error performing fetch: %@", error);
}
}
return fetchedResultsController;
}
使bob成为常规对象。
您的var bob = new Object()
函数接受参数。它将setAge
设置为该值。但是setAge放在全局范围内。因此this.age
- &gt; this
。这就是为什么当你在全球范围内输入window
时你得到50。
Jaromanda是正确的,您需要将函数age
放在对象bob上。
如果你正在玩setAge
,你可能会对伪古典实例化风格感兴趣:
this
答案 1 :(得分:4)
bob.setAge = setAge(50);
所做的就是将bob的setAge属性设置为调用setAge(50)的结果 - 因为setAge retunrs undefined,你有效地设置了bob.SetAge = undefined。也许你打算做的是
bob.setAge = setAge;
bob.setAge(50);
未经测试,但我认为这是对的
答案 2 :(得分:1)
关键字 this 充当占位符,会在实际使用该方法时引用该方法的任何对象。
查看方法setAge以了解其工作原理。通过使用关键字此, setAge 将更改调用它的任何对象的age属性。
然后我们说 bob.setAge = setAge ;这意味着只要我们在 setAge 方法中键入 bob.setAge() this.age ,就会引用 bob.age。强>