简体   繁体   English

Javascript中的原型

[英]Prototypes in Javascript

Even though I have read a lot about it I can't get my head around the prototype concept. 虽然我已经阅读了很多关于它的内容,但我无法理解原型概念。

Why is there a String and String.prototype ? 为什么有StringString.prototype

If I have "cat" : 如果我有"cat"

  • Is that a String or an Object ? 那是一个String还是一个Object
  • Does it inherits all properties/methods from String or String.prototype ? 它是否从StringString.prototype继承所有属性/方法?
  • Why is there a String and String.prototype ? 为什么有StringString.prototype
  • Should I call String the String object and String.prototype the String prototype? 我应该调用String的String对象和String.prototype的String原型吗?

Please bring clarity to this. 请清楚这一点。

I'm answering this because there is a lot of misinformation in the subject: 我正在回答这个问题,因为这个主题有很多错误的信息:

Is that a String or an Object? 那是一个字符串还是一个对象?

No "cat" is a primitive String value : 没有 "cat"原始字符串值

typeof "cat"; // "string", a String value
"cat" instanceof String; // false


typeof new String("cat"); // "object", a String object
new String("cat") instanceof String; // true

I will talk later on about types and primitive values. 我稍后会谈到类型和原始值。

Does it inherits all properties/methods from String or String.prototype? 它是否从String或String.prototype继承所有属性/方法?

Well, when you use the property accessor operator (the dot or the bracket notation), the primitive value is implicitly converted to object, internally, therefore all the methods on String.prototype are available, for example: 好吧,当你使用属性访问器运算符 (点或括号表示法)时,原始值在内部隐式转换为对象,因此String.prototype上的所有方法都可用,例如:

When you access: 当您访问:

"cat".chatAt(0);

Behind the scenes "cat" is converted to object: 在幕后"cat"被转换为对象:

Object("cat").chatAt(0);

That's why you have access to all the inherited properties on values. 这就是您可以访问值的所有继承属性的原因。

Why is there a String and String.prototype? 为什么有String和String.prototype?

String is a constructor function, allows you to create String objects or do type conversion: String是一个构造函数,允许您创建String对象或进行类型转换:

var stringObj = new String("foo"); // String object

// Type conversion
var myObj = { toString: function () { return "foo!"; } };

alert(String(myObj)); // "foo!"

The String.prototype object, is the object where String object instances inherit from. String.prototype对象是String对象实例继承的对象。

I know it's confusing, we have String values and String objects, but most of the time you actually work only with string values, don't worry for now about String objects. 我知道它很混乱,我们有String值和String对象,但大多数时候你实际上只使用字符串值,现在不要担心String对象。

Should I call String the String object and String.prototype the String prototype? 我应该调用String的String对象和String.prototype的String原型吗?

You should call String "The String constructor" . 你应该调用String “The String constructor”

"String prototype" is ok. “字符串原型”没问题。

You should know that "Everything is NOT an object". 你应该知道,“一切是不是对象”。

Let's talk about types , there are five language types specified: 我们来谈谈类型 ,指定了五种语言类型

  • String
  • Number
  • Boolean 布尔
  • Null 空值
  • Undefined 未定义

A primitive value is " a datum that is represented directly at the lowest level of the language implementation", the simplest piece of information you can have. 原始值是“直接表示在语言实现的最低级别的数据”,这是您可以拥有的最简单的信息。

The values of the previously described types can be: 先前描述的类型的值可以是:

  • Null: The value null . Null:值null
  • Undefined: The value undefined . 未定义:值undefined
  • Number: All numbers, such as 0 , 3.1416 , 1000 , etc.. Also NaN , and Infinity . 号码:所有号码,如03.14161000 ,等等。也NaN ,并Infinity
  • Boolean: The values true and false . Boolean:值truefalse
  • String: Every string, such as "cat" and "bar" . String:每个字符串,例如"cat""bar"

Strings are Objects in JavaScript. 字符串是JavaScript中的对象。 There is no real datatype called string like in PHP, C, or other languages. 在PHP,C或其他语言中没有称为字符串的真实数据类型。

String.prototype ist a construct to add functionality to all your String-Objects. String.prototype是一个向所有String-Objects添加功能的构造。 If you want all Strings to offer for example a conversion function you do the following: 如果您希望所有字符串都提供转换功能,请执行以下操作:

String.prototype.convert = function () {
   // Put your code in here
}

// Set up y new String
var example = "cat";

// Now you can call your function
example.convert();

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

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