Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

javascript - How does jQuery makes the jQuery object both a function and an object property?

I've been wondering for a long time how jQuery can be both a function and an object property.

You can use it like a function, jQuery(...) and you can use it like a property jQuery.ajax(...)

How can you achieve a such thing in Javascript?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

functions are objects in javascript. So you can have your main function

var $ = function() {  alert('wat'); }

and then extend it

$.fadeTo = function() { alert('fadeto'); }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...