JQuery source code questions

前端 未结 2 395
天命终不由人
天命终不由人 2021-01-22 08:54

I got two questions about following code snippet.

(1). What is the purpose of \"return new jQuery.fn.init( selector, context, rootjQuery );\"? Why does it return another

2条回答
  •  终归单人心
    2021-01-22 09:12

    1. When JQuery is called as an ordinary function, a new (class) instance of JQuery is created and returned using new JQuery.fn.init(...). In this way, developers don't have to add the new keyword before $(..).
    2. JQuery.fn is a shortcut for JQuery.prototype. Writing JQuery.fn.customMethod = function(){...} is more convenient than writing JQuery.prototype.customMethod = .... Because JQuery is often also accessible through $ or $j, The shortesy way to refer to JQuery.prototype is $.fn.

提交回复
热议问题