繁体   English   中英

试图理解javascript Function.prototype

[英]trying to understand javascript Function.prototype

我试图理解这段代码

Function.prototype.method = function (name, fn) {
    this.prototype[name] = fn;
    return this;
};

函数体中“this”指的是什么? 它引用了Function.prototype吗? 是否尝试将成员添加到Function.prototype.prototype ,即Function.prototype.prototype[name]

Function.prototype中的Function.prototypeFunction实例上调用。
因此, this是指您调用它的功能。

this.prototype将引用您调用它的函数的原型。

例如:

function MyClass() { }
MyClass.method("myMethod", function() { });

var c = new MyClass();
c.myMethod();    //MyClass.prototype.myMethod

“this”指的是您在赋值语句右侧创建的新函数。

>     Function.prototype.method = function (name, fn) {
>         this.prototype[name] = fn;
>         return this;
>     };

这意味着函数从Function.prototype继承方法属性。 例如

  function Foo(){}
  alert(typeof Foo.method); // function

当调用Foo.method()然后在方法函数中, this将引用Foo ,因此:

  Foo.method('sayHi', function(){alert('hi');});

创建Foo.prototypesayHi属性并为其指定所提供函数的值。 请注意, Foo不会继承方法指定的函数,只有Foo创建的对象(即Foo的实例)才具有该方法。

笔记

函数的this关键字是由调用设置的,所以如果你以其他方式调用方法,它可能会出现异常:

  var c = { method: Foo.method };
  alert(typeof c.method); // function

  c.method('sayHi', function(){alert('hi');}); // this.prototype is undefined

函数对象与函数实例对象

首先,在javascript中,函数也是一个对象。 从这里,我的意思不是new()构造创建的对象,而是函数本身。 为了避免混淆,我会将这些对象称为Function对象 ,并将使用new()构造函数创建的对象称为Function实例对象

_ proto _和原型属性

in javascript has two properties: _ proto _ and prototype . javascript中的任何都有两个属性: _ proto _prototype (created using new constructor) has a property _ proto _ . 此外,任何 (使用新构造创建)都具有属性_proto _ _proto _是定义继承的原因。 可以找到一些关于此的好资源

http://zeekat.nl/articles/constructors-considered-mildly-confusing.html

如何定义继承?

如果objA和objC通过任意数量的_proto _连接,则对象objA继承另一个对象objC。 因此,如果objA具有_ proto _等于objB,并且objB具有_ proto _等于objC,则objA继承objB和objC,而objB继承objC。

继承是什么意思?

any property of inherited object. 这意味着任何继承对象都可以继承对象的任何属性。

什么是Function.prototype

refers. 它是每个 _ proto _所指的 has access to properties of Function.prototype, since every inherits Function.prototype object. 这意味着每个都可以访问Function.prototype的属性,因为每个继承Function.prototype对象。 property is added to Function.prototype object, it would be available to all the possible in javascript. 这也意味着如果将属性添加到Function.prototype对象,它将可用于javascript中所有可能的 这包括字符串,数字等。

when the 'method' is invoked from s like Number, String, etc. Which means that we now have a new property in with name "name", and its a function 'func'. “这”指的是当“方法”是从就像数字,字符串等调用这意味着我们现在在的新属性名称为“名”,它的功能“功能” 。

property of 属性有什么

's prototype is referred to by the 's _ proto _ created using that function's new construct. 原型由使用该函数的新构造创建的_proto _

如果执行以下操作:

Number.method('integer',function(){...});

然后Number.prototype中定义了整数方法。 , eg new Number (2.4), would "inherit" this new property 'integer' from Number.prototype, since that Number would have its _ proto _ set to Number.prototype. 这意味着每个Number ,例如new Number(2.4),将从Number.prototype“继承”这个新属性'integer',因为该Number 将其_ proto _设置为Number.prototype。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM