2014-01-28 96 views
0

这是一个非常激烈的问题,但我认为其原因在于缺乏有关此主题的良好文档和优秀书籍。javaScript中的对象,原型和构造函数

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

我以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.prototype.constructor == car.constructor ==车辆()//括号这里

Vehicle.constructor ==功能()//大写 'F'
Vehicle.constructor.prototype ==函数()//小写 'F'

car.prototype ==未定义未定义//但没有报告作为误差
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'是只有不确定的,但“车.prototype.constructor'是未定义的,并且也是'TypeError'

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

所有这一切都让我感到困惑。

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



编辑

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

Vehicle.constructor.prototype:函数()
Vehicle.prototype.constructor:车辆()
car.constructor。原型:车辆{}

+0

我很难理解你的问题。你到底在问什么?我们可以回答“原型和构造函数之间有什么区别”或者“为什么x.y.z给出TypeError时x.y没有定义”,但是你没有问任何特定的问题。对我来说,最好的办法是找到关于JavaScript的函数,原型和构造函数的好文章,并理解它们之间的关系。你可以尝试[this](http://www.codeproject.com/Articles/687093/Understanding-JavaScript-Object-Creation-Patterns),尽管还有其他无数人。 – Avish

+0

基本上我不明白原型和构造者是如何不同的,为什么我们既可以与对象和函数相关联,也可以使用object.prototype.constructor(或反函数) – user3244003

回答

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/

0

你应该永远记住:JavaScript中的每个函数实际上是一个函数对象,从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 

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