sql执行顺序

2015/10/25

The difference between jQuery's bind() live() and delegate()

.bind()

$('a').bind('click', function() { alert("That tickles!") });

jQuery scans the document for all $('a') elements and binds the alert function to each of their click events

.live()

$('a').live('click', function() { alert("That tickles!") });

jQuery binds the alert function to the $(document) element, along with click and a as parameters. Anytime an event bubbles up to the document node, it checks to see if the event was a click and if the target element of that event matches the a CSS selector.

The live method can also be bound to a specific element other that document

$('a', $('#container')[0]).live(...);

.delegate()

$('#container').delegate('a', 'click', function() { alert("That tickles!") });

jQuery scans the document for $('#container'), and binds the alert function along with the click event and a CSS selector as parameters. Anytime an event bubbles up to $('#container'), it checks to see if the event was a click and if the target element of that event matches the CSS selector.

Why .delegate() is better than .live()

Why .live() or .delegate() instead of .bind()

There are 2 reasons we prefer delegate or live to bind:

  • To attach handlers to DOM elements that may not yet exist in the DOM. Because bind directly binds handlers to the individual element, it cannot bind them to elements that aren't on the page yet.

If you were to run $('a').bind(...), and then new links were added to the page via AJAX, your bind handler would not work for these.

  • Or to attach a handler to a single element or small group of elements, listening for events on descendent elements, instead of looping through and attaching the same function to 100 individual elements in the DOM. This would be the performance benefit of attaching a handler to one ancestor element(s) instead of directly attaching handlers to all elements on the page.

Reference

http://www.alfajango.com/blog/the-difference-between-jquerys-bind-live-and-delegate/

2015/10/25

OAUTH和OPENID

2015/10/25

OAUTH和OPENID 2

2015/10/25

Java杂记

If I have a try/catch block with returns inside it, will the finally block be called?

finally will be called.
The only time finally won't be called is if you call System.exit() or if the JVM crashes first.

final-修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出心的子类,不能作为父类被继承。因此一个类不能既被声明为abstract,又被声明为final。

将变量或方法声明为final,可以保证他们在使用中不被改变。

被声明为final的变量必须在声明时给定初值,而在以后的引用中只能读取,不可修改。

被声明为final的方法也同样只能使用,不能重载。

几种情况

打印“123123”,指针指向‘STRING_BUILDER’没变,但是‘STRING_BUILDER’对象的值更改了

static final StringBuilder STRING_BUILDER = new StringBuilder("123");

@Test
public void stringBuilderTest1()
{
    STRING_BUILDER.append("123");
    System.out.println(STRING_BUILDER);
}

STRING_BUILDER新建一个对象是不可以的

static final StringBuilder STRING_BUILDER = new StringBuilder("123");

@Test
public void stringBuilderTest2()
{
    // error 不可以
    STRING_BUILDER = new StringBuilder("8888");
    System.out.println(STRING_BUILDER);
}

‘STRING_BUILDER’之前指向的是‘tempbu’‘tempbu’对应的值是‘123’

而后‘tempbu’对应了一个新的值‘8888’,但是‘STRING_BUILDER’依然指向原来‘tempbu’对应的位置

static StringBuilder tempbu = new StringBuilder("123");
static final StringBuilder STRING_BUILDER = tempbu;

@Test
public void stringBuilderTest3()
{
    tempbu = new StringBuilder("8888");
    System.out.println(STRING_BUILDER);
}

所以一个对象即使有‘final’关键词修饰,他的值也是可以变化的,只是不能指向一个新的对象(即指向对象不能变,指向对象的值可以变)


HashMap和Hashtable的区别。

都属于Map接口的类,实现了将惟一键映射到特定的值上。

HashMap允许一个 null 键和多个 null 值。

Hashtable 类似于 HashMap,但是不允许 null 键和 null 值。它也比 HashMap 慢,因为它是同步的。


short s1 = 1; s1 = s1 + 1;有错,s1short型,s1+1int型,不能显式转化为short型。可修改为s1 =(short)(s1 + 1)

short s1 = 1; s1 += 1正确。


sleep()wait()有什么区别?

sleep()方法是使线程停止一段时间的方法。在sleep 时间间隔期满后,线程不一定立即恢复执行。这是因为在那个时刻,其它线程可能正在运行而且没有被调度为放弃执行,除非(a)“醒来”的线程具有更高的优先级
(b)正在运行的线程因为其它原因而阻塞。

wait()是线程交互时,如果线程对一个同步对象x 发出一个wait()调用,该线程会暂停执行,被调对象进入等待状态,直到被唤醒或等待时间到。


  • ++i will increment the value of i, and then return the incremented value.

    i = 1;
    j = ++i;
    (i is 2, j is 2)
    
  • i++ will increment the value of i, but return the pre-incremented value.

    i = 1;
    j = i++;
    (i is 2, j is 1) 
    
    int index = 10;
    
    System.out.println(index--);//10
    
    System.out.println(--index);//9
    

Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference, it passes them by value.

public void badSwap(int var1, int var2)
{
  int temp = var1;
  var1 = var2;
  var2 = temp;
}

When badSwap() returns, the variables passed as arguments will still hold their original values.

Here is where it gets tricky:

public void tricky(Point arg1, Point arg2)
{
  arg1.x = 100;
  arg1.y = 100;
  Point temp = arg1;
  arg1 = arg2;
  arg2 = temp;
}
public static void main(String [] args)
{
  Point pnt1 = new Point(0,0);
  Point pnt2 = new Point(0,0);
  System.out.println("X: " + pnt1.x + " Y: " +pnt1.y); 
  System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
  System.out.println(" ");
  tricky(pnt1,pnt2);
  System.out.println("X: " + pnt1.x + " Y:" + pnt1.y); 
  System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);  
}

If we execute this main() method, we see the following output:

X: 0 Y: 0
X: 0 Y: 0
X: 100 Y: 100
X: 0 Y: 0

2015/10/25