Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 价值、原型和属性的差异_Javascript_Jquery_Oop_Reverse Engineering - Fatal编程技术网

Javascript 价值、原型和属性的差异

Javascript 价值、原型和属性的差异,javascript,jquery,oop,reverse-engineering,Javascript,Jquery,Oop,Reverse Engineering,好的!首先,这个问题来自一个在jQuery世界中挖得太深(可能会迷路)的人 在我的研究中,我发现jquery的主要模式是这样的(如果需要的话,请更正): 当$启动jQuery.prototype.init时,启动并返回一个元素数组。但我无法理解它是如何将jQuery方法(如.css或.hide等)添加到此数组的 我得到了静态方法。但是无法获得它如何使用所有这些方法返回和元素数组。我也不喜欢这种模式。它们有一个init函数,它是所有jQuery实例的构造函数,jQuery函数本身只是对象创建的一个

好的!首先,这个问题来自一个在jQuery世界中挖得太深(可能会迷路)的人

在我的研究中,我发现jquery的主要模式是这样的(如果需要的话,请更正):

$
启动
jQuery.prototype.init
时,启动并返回一个元素数组。但我无法理解它是如何将jQuery方法(如
.css
.hide
等)添加到此数组的


我得到了静态方法。但是无法获得它如何使用所有这些方法返回和元素数组。

我也不喜欢这种模式。它们有一个
init
函数,它是所有jQuery实例的构造函数,
jQuery
函数本身只是对象创建的一个包装,使用
new

function jQuery(…) { return new init(…); }
然后,他们将这些实例的方法添加到
init.prototype
对象中。此对象作为接口公开在
jQuery.fn
。此外,他们还将jQuery函数的
prototype
属性设置为该对象-对于那些不使用
fn
属性的人。现在你有了

jQuery.prototype = jQuery.fn = […]init.prototype
但他们也做了两件[奇怪的]事情:

  • 覆盖原型对象的
    constructor
    属性,将其设置为
    jQuery
    函数
  • jQuery.fn
    上公开
    init
    函数—它自己的原型。这可能是允许的,但非常混乱

我认为他们需要/想要做所有这些以防愚蠢,但他们的代码是一团乱——从对象文字开始,然后分配init原型。

如果您将API视为方法的外部集合,将jQuery函数视为包装器,那么更容易理解

它基本上是这样构造的:

function a() { return new b();}
a.prototype.method = function() { return this; }
function b() {}
b.prototype = a.prototype;
function a(){}
function b(){}
a.prototype = b.prototype;
console.log( new b instanceof a); // true
console.log( new a instanceof b); // true
除了
a
jQuery
b
jQuery.prototype.init

我确信Resig在init原型中放置api构造函数是有原因的,但我看不到。除了Bergi提到的以外,还有一些奇怪的地方:

1) 模式需要从
jQuery.fn.init.prototype
jQuery.prototype
的引用副本,这允许一个奇怪的无休止循环:

var $body = new $.fn.init.prototype.init.prototype.init.prototype.init('body');
2) 每个jQuery集合实际上都是
jQuery.fn.init
的一个实例,但由于它们引用了相同的原型对象,这就让我们“认为”集合是
jQuery
的一个实例。你也可以这样做:

function a() { return new b();}
a.prototype.method = function() { return this; }
function b() {}
b.prototype = a.prototype;
function a(){}
function b(){}
a.prototype = b.prototype;
console.log( new b instanceof a); // true
console.log( new a instanceof b); // true
旁注:我个人使用了以下构造函数模式,具有类似的结果,但并不奇怪:

var a = function(arg) {
    if (!(this instanceof a)) {
        return new a(arg);
    }
};
a.prototype.method = function(){ return this; };

是否过度编写了构造函数如何获取
this
,即返回的值指向jQuery?…不,这就是#2奇怪的东西的用途…什么是#1奇怪的东西?这本质上是一个让inits
this
看起来像jQuery
this
不,原型对象上的
构造函数
属性与功能无关。我不知道你说“让inits
this
看起来像jQuery
this
”是什么意思-这只是通常的“类”模式别忘了无尽的原型循环,f.ex:
new$.fn.init.prototype.init.prototype.init.init.prototype.init('body')当然,尽管你也有类似的反引用,
…prototype.constructor…