javaScript中的对象,原型和cosntructors

时间:2014-01-28 10:54:07

标签: javascript oop

这是一个非常争论的问题,但我认为原因是缺乏关于这一主题的良好文档和好书。

当我学习一门语言时,即使我的一些测试是荒谬的,我也会进行大量挖掘,但我认为理解事物的唯一方法就是进行各种实验。

我开始使用JavaScript,而且我的代码也越多,我就越困惑。

让我们查看代码(暂时忘记无意义,只考虑输出):

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

我的结论:

Vehicle.prototype == car.constructor.prototype == Vehicle {} //花括号在这里
Vehicle.prototype.constructor == car.constructor == Vehicle()//括号在这里

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

car.prototype == undefined //未定义但未报告为错误
car.prototype.constructor //输入错误。这里'car.prototype'被报告为错误

现在让我们考虑类似的代码:

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

正如我们所看到的,这里'car.prototype'仅 未定义,但'car.prototype.constructor'未定义且'TypeError'

上面的代码在Firebug中进行了测试。我不知道这是Firebug的错还是JavaScript错误。

所有这些让我感到困惑。

如果这是JavaScript中的OOP,我认为这是更多ODP - 面向对象(Dis)的编程



修改

1)当car.prototype.constructor为TypeError时,为什么car.prototype未定义 2)为什么函数和对象都有构造函数和原型(参见上面的代码)? 3)这是什么意思:

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

2 个答案:

答案 0 :(得分:0)

每次使用对象文字表示法声明对象时

var newObject = {foo: 'bar'};

您正在创建一个继承自'Object()'的对象,并且没有自己的原型。

请注意,当您致电'car.constructor'时,它会返回'Object()'。这是因为这是JS在对象文字表示法中使用的构造函数。

当你调用'car.prototype'时,它是未定义的,因为对象文字没有自己的原型。

当你调用'car.constructor.prototype'时,它会返回一个空对象,因为它基本上是'Object()'的值。它只是创建对象。

为了让你的对象拥有自己的原型,你必须建立一个构造函数并使用

'new myObject()'声明您的对象。

我希望我已经帮助回答了你的问题。以下是一些包含更多信息的链接:

Adding Prototype to JavaScript Object Literal

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

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

答案 1 :(得分:0)

你应该永远记住:JavaScript中的每个函数实际上都是一个Function对象,因为Function继承自Object,任何函数也都是Object。所以,请记住这句话,看看这个:

(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

因此,不同的大括号表明原型始终是一个对象(花括号),如果它被定义,构造函数总是一个函数。就是这样。

相关问题