为什么原型未定义 [英] why prototype is undefined
问题描述
我知道这已经被问过数百次了,但是,我似乎无法理解prototype
I known this has been asked hundreds of times, however, I can't seem to grasp the concept of prototype
这是我的示例脚本
var config = {
writable: true,
enumerable: true,
configurable: true
};
var defineProperty = function(obj, name, value) {
config.value = value;
Object.defineProperty(obj, name, config);
}
var man= Object.create(null);
defineProperty(man, 'sex', "male");
var person = Object.create(man);
person.greet = function (person) {
return this.name + ': Why, hello there, ' + person + '.'
}
var p=Object.getPrototypeOf(person);
alert(p.sex);//shows male
person.prototype.age=13;//why there is a error said the prototype is undefined? I thought it supposed be man object...
var child=function(){}
child.prototype.color="red";//why this line doesn't show error? both child and person are an object .
alert(child.prototype.color);//shows red
var ch=Object.getPrototypeOf(child);
alert(ch.color);//why it is undefined? it is supposed red.
希望你能给我一些帮助......谢谢.
Hope you can give me some helps... thanks.
更新:
感谢你们的帮助,根据 Elclanrs 的回答,以下是我学到的.
Thanks your guys kindly help, Based on Elclanrs's answer, Below is what I learned.
Function
是 javascript 中的内置对象之一.3种格式创建函数对象相等.
Function
is the one of the build-in objects in javascript. the 3 format creation function object are equal.
var function_name = new Function(arg1, arg2, ..., argN, function_body)
function function_name(arg1, arg2, ..., argN)
{
...
}
var function_name=function(arg1, arg2, ..., argN)
{
...
}
所以,这就是为什么要创建一个原型链,我们必须创建一个函数,然后使用 new 关键字调用它.
So, that is why create a prototype chain we have to create a function and then call it with the new keyword .
Function.prototype
是对所有 Function 对象 prototype
的引用.
Function.prototype
is the reference to All the Function object prototype
.
干杯
推荐答案
我认为您可能混淆了概念.先尝试通过经典的原型继承来掌握原型的概念,然后你就可以接触到所有新的Object
东西.
I think you might be mixing concepts. Try grasping the concept of prototypes with classic prototype inheritance first, then you can get into all the new Object
stuff.
在 JavaScript 中,每个对象(数字、字符串、对象、函数、数组、正则表达式、日期...)都有一个 prototype
,您可以将其视为方法(函数)的集合,对该对象的所有当前和未来实例通用.
In JavaScript, every object (numbers, strings, objects, functions, arrays, regex, dates...) has a prototype
which you can think of as a collection of methods (functions) that are common to all current and future instances of that object.
要创建原型链,您必须创建一个函数,然后使用 new
关键字调用它以指定它是一个构造函数.您可以将构造函数视为主函数,它接受构建对象的新实例所需的参数.
To create a prototype chain you have to create a function and then call it with the new
keyword to specify that it is a constructor. You can think of constructors as the main function that takes the parameters necessary to build new instances of your object.
考虑到这一点,您可以扩展本机对象或创建自己的新原型链.这类似于类的概念,但在实践中更强大.
Having this in mind, you can extend native objects or create your own new prototype chains. This is similar to the concept of classes but much more powerful in practice.
与您的示例类似,您可以编写这样的原型链:
Similar to your example, you could write a prototype chain like this:
// Very basic helper to extend prototypes of objects
// I'm attaching this method to the Function prototype
// so it'll be available for every function
Function.prototype.inherits = function(parent) {
this.prototype = Object.create(parent.prototype);
}
// Person constructor
function Person(name, age, sex) {
// Common to all Persons
this.name = name;
this.age = age;
this.sex = sex;
}
Person.prototype = {
// common to all Persons
say: function(words) {
return this.name +'says: '+ words;
}
};
// Student constructor
function Student(name, age, sex, school) {
// Set the variables on the parent object Person
// using Student as a context.
// This is similar to what other laguanges call 'super'
Person.call(this, name, age, sex);
this.school = school; // unique to Student
}
Student.inherits(Person); // inherit the prototype of Person
var mike = new Student('Mike', 25, 'male', 'Downtown'); // create new student
console.log(mike.say('hello world')); //=> "Mike says: hello world"
在较新版本的 JavaScript(阅读 EcmaScript)中,他们添加了处理对象和扩展对象的新方法.但是它的概念与经典的原型继承有点不同,它似乎更复杂,并且更多地了解 JS 在底层如何工作将有助于真正理解它是如何工作的,而且它在旧浏览器中不起作用.这就是为什么我建议你从经典模式开始,你可以在互联网上找到准确和丰富的信息.
In newer version of JavaScript (read EcmaScript) they added new ways to deal with objects and extend them. But the concept it's a bit different from classical prototype inheritance, it seems more complicated, and some more knowledge of how JS works underneath would help to really understand how it works, plus it doesn't work in older browsers. That's why I suggest you start with the classical pattern for which you'll find accurate and abundant information on the internet.
这篇关于为什么原型未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!