简体   繁体   English

javaScript中的对象,原型和辅助结构

[英]Objects, prototypes and cosntructors in javaScript

This is a kind of question very debated, but I think the reason is the lack of good documentation and good books on the subject. 这是一个争议很大的问题,但我认为原因是缺乏有关该主题的好的文档和书籍。

When I study a language I do a lot of digging even if some of my tests are absurd, but I think the only way to understand things is doing all kinds of experiments. 当我学习一门语言时,即使我的某些测试是荒谬的,我也会做很多事情,但是我认为理解事物的唯一方法是进行各种实验。

I'm beginning with JavaScript and as much I code as more I'm confused. 我从JavaScript入手,我编写的代码越来越多,感到困惑。

Let's view the code (forget the no-senses by now and consider only the outputs): 让我们查看代码(现在忘记废话,只考虑输出):

function Vehicle()
{
    this.color = 'Blue'
}

var car = new Vehicle()

console.log('Vehicle.constructor: ', Vehicle.constructor)
console.log('car.constructor: ', car.constructor)

console.log('Vehicle.prototype: ', Vehicle.prototype)
console.log('car.prototype: ', car.prototype)

console.log('Vehicle.constructor.prototype: ', Vehicle.constructor.prototype)
console.log('car.constructor.prototype: ', car.constructor.prototype)

console.log('Vehicle.prototype.constructor: ', Vehicle.prototype.constructor)
console.log('car.prototype.constructor: ', car.prototype.constructor)



Output:

Vehicle.constructor: Function()
car.constructor: Vehicle()

Vehicle.prototype: Vehicle {}  // curly braces here
car.prototype: undefined

Vehicle.constructor.prototype: function()
car.constructor.prototype: Vehicle {}      // curly braces here

Vehicle.prototype.constructor: Vehicle()
TypeError: car.prototype is undefined

My conclusions: 我的结论:

Vehicle.prototype == car.constructor.prototype == Vehicle {} // curly braces here Vehicle.prototype == car.constructor.prototype == Vehicle {} //这里的花括号
Vehicle.prototype.constructor == car.constructor == Vehicle() // parenthesis here Vehicle.prototype.constructor == car.constructor == Vehicle()//括号在这里

Vehicle.constructor == Function() // uppercase 'F' Vehicle.constructor == Function()//大写字母'F'
Vehicle.constructor.prototype == function() // lowercase 'f' Vehicle.constructor.prototype == function()//小写字母'f'

car.prototype == undefined // undefined but not reported as error car.prototype ==未定义//未定义,但未报告为错误
car.prototype.constructor // Type error. car.prototype.constructor //输入错误。 Here 'car.prototype' is reported as an error 这里“ car.prototype”被报告为错误

Now let's consider the similar code: 现在让我们考虑类似的代码:

var car = {color: 'Blue'}

console.log('car.constructor: ', car.constructor)
console.log('car.prototype: ', car.prototype)
console.log('car.constructor.prototype: ', car.constructor.prototype)
console.log('car.prototype.constructor: ', car.prototype.constructor)


Output: 

car.constructor: Object()
car.prototype: undefined
car.constructor.prototype: Object {}  // curly braces here
TypeError: car.prototype is undefined

As we can see, here 'car.prototype' is only undefined, but 'car.prototype.constructor' is undefined and also 'TypeError' 正如我们所看到的,这里的car.prototype“ 仅仅是不确定的,但“car.prototype.constructor”是不确定的, 也是 “类型错误”

The code above is tested in Firebug. 上面的代码在Firebug中进行了测试。 I don't know this is Firebug's fault or JavaScript fault. 我不知道这是Firebug的错误还是JavaScript的错误。

All of this puzzled me. 这一切使我感到困惑。

If this is OOP in JavaScript I think this is more ODP - Object (Dis)oriented Programming 如果这是OOP在JavaScript中,我认为这是更ODP - 面向对象(DIS)编程



EDIT 编辑

1) Why car.prototype is undefined when car.prototype.constructor is TypeError 1)为什么当car.prototype.constructor为TypeError时car.prototype未定义
2) Why both functions and objects have constructors and prototypes (see code above)? 2)为什么两种功能和对象具有构造和原型(见上面的代码)?
3) What the meaning of this: 3)这是什么意思:

Vehicle.constructor.prototype: function() Vehicle.constructor.prototype:function()
Vehicle.prototype.constructor: Vehicle() Vehicle.prototype.constructor:Vehicle()
car.constructor.prototype: Vehicle {} car.constructor.prototype:车辆{}

Any time you declare an object using object literal notation 任何时候使用对象文字符号声明对象

var newObject = {foo: 'bar'};

You are creating an object that inherits from 'Object()' and has no prototype of it's own. 您正在创建一个继承自'Object()' ,但没有其原型。

Notice when you call 'car.constructor' it returns 'Object()' . 注意,当您调用'car.constructor'它会返回'Object()' This is because that is the constructor JS uses in object literal notation. 这是因为这是JS在对象文字表示法中使用的构造函数。

When you call 'car.prototype' it is undefined because object literals have no prototype of their own. 当您调用'car.prototype'它是未定义的,因为对象文字本身没有原型。

When you call 'car.constructor.prototype' it returns an empty object since that is the essentially the value of 'Object()' . 当您调用'car.constructor.prototype'它会返回一个空对象,因为这实际上是'Object()'的值。 It just creates objects. 它只是创建对象。

in order for your objects to have their own prototypes you must establish a constructor and use 为了使您的对象拥有自己的原型,您必须建立一个构造函数并使用

'new myObject()' to declare your objects. 'new myObject()'声明您的对象。

I hope I've helped answer your question. 希望我能帮助您回答问题。 Here are a few links that have more info: 这里是一些具有更多信息的链接:

Adding Prototype to JavaScript Object Literal 向JavaScript对象文字添加原型

http://doctrina.org/Javascript-Objects-Prototypes.html http://doctrina.org/Javascript-Objects-Prototypes.html

http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/ http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/

You should remember forever: Every function in JavaScript is actually a Function object, as Function inherited from Object, any function is Object too. 您应该永远记住:JavaScript中的每个函数实际上都是一个Function对象,因为Function继承自Object,所以任何函数也是Object。 So, keeping in mind this sentence, look at this: 因此,请记住以下句子:

(function f(){}) instanceof Function;        // true
(function f(){}) instanceof Object;          // true, as Function is an instance of Object
({}) instanceof Object;                      // true
({}) instanceof Function;                    // false!

Function.prototype instanceof Object;        // true
Function instanceof Object;                  // true, as all objects in JS are inherited     from Object
Object instanceof Function;                  // true, as Object is a constructor function, therefor:
Function instanceof Function;                // true
Object instanceof Object;                    // true, but:
Date instanceof Date;                        // false

Function.prototype.constructor === Function; // true
Function.constructor === Function;           // true     

typeof Object === 'function';                // true, as Object is a constructor function

So, different braces indicate that prototype is always an object (curly braces), if it is defined, and constructor is always a function. 因此,不同的花括号表示原型(如果已定义)始终是一个对象(花括号),而构造函数始终是一个函数。 That's all. 就这样。

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

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