Javascript 为什么原型是未定义的
我知道这已经被问了几百次了,但是,我似乎无法理解Javascript 为什么原型是未定义的,javascript,prototype,Javascript,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= Ob
原型的概念
这是我的示例脚本
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.
希望你能给我一些帮助。。。谢谢
更新:
感谢你们的帮助,根据埃尔克兰斯的回答,下面是我学到的
Function
是javascript中的内置对象之一。3个格式创建函数对象相等
var function_name = new Function(arg1, arg2, ..., argN, function_body)
function function_name(arg1, arg2, ..., argN)
{
...
}
var function_name=function(arg1, arg2, ..., argN)
{
...
}
所以,这就是为什么要创建一个原型链,我们必须创建一个函数,然后用new关键字调用它
Function.prototype
是对所有函数对象的引用prototype
Cheers原型
属性只存在于函数上,而人不是函数。它是一个对象
下面是正在发生的事情:
var man = Object.create(null); // man (object) -> null
man.sex = "male";
var person = Object.create(man); // person (object) -> man (object) -> null
person.greet = function () { ... };
var p = Object.getPrototypeOf(person); // man (object) -> null
alert(p.sex); // p is the same object as man
person.prototype.age = 13; // person doesn't have a prototype
var child = function () {}; // child (function) -> Function.prototype
// -> Object.prototype -> null
child.prototype.color = "red"; // child has a prototype
var ch = Object.getPrototypeOf(child); // Function.prototype
alert(ch.color); // ch is not the same as color.prototype
// ch is Function.prototype
有关更多信息,我建议您阅读以下答案:
编辑:用尽可能少的话解释正在发生的事情:
null
和undefined
[[proto]]
的属性,程序员无法访问该属性。但是,大多数引擎将此属性作为\uuuu proto\uuu
进行访问varo={a:false,b:“something”,…}
这样的对象时,那么o.\uuuu proto\uuuu
就是object.prototype
var o=object.create(something)
这样的对象时,那么o.\uuu proto\uuu
就是something
var o=newf(a,b,…)
这样的对象时,那么o.。\uuuu proto\uuu
就是f.prototype
o
上找不到属性时,它会在o.\uuuuu proto\uuuuu
上搜索属性,然后在o.\uuuuuu proto\uuuuuuu
上搜索属性,直到找到属性或原型链以null
结尾(在这种情况下,属性是未定义的)
Object.getPrototypeOf(o)
返回o.\uuuu-proto\uuuu
而不是o.prototype
-\uuu-proto\uuu
存在于包括函数在内的所有对象上,但prototype
仅存在于函数上我想你可能混淆了一些概念。首先尝试通过经典原型继承来掌握原型的概念,然后您就可以了解所有新的
对象
内容
在JavaScript中,每个对象(数字、字符串、对象、函数、数组、正则表达式、日期…)都有一个原型,您可以将其视为该对象所有当前和未来实例所共有的方法(函数)的集合
要创建原型链,您必须创建一个函数,然后使用new
关键字调用它,以指定它是一个构造函数。您可以将构造函数视为获取构建对象新实例所需参数的主要函数
考虑到这一点,您可以扩展本机对象或创建自己的新原型链。这类似于类的概念,但在实践中更强大
与您的示例类似,您可以编写如下原型链:
// 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的底层工作原理将有助于真正理解它是如何工作的,而且它在较旧的浏览器中不起作用。这就是为什么我建议您从经典模式开始,您可以在互联网上找到准确而丰富的信息。感谢您友好的回答和帮助。我无法再投票了。我更新了我的帖子。我有个问题要问你。在我可以使用.prototype
?@Joe.wang之前,是否需要使用new
关键字调用?您可以调用构造函数的.prototype属性(通常只返回对象),或者必须使用new with构造函数创建对象的新实例。完成后,您可以通过查看实例的构造函数来获得原型。prototype这部分回答了问题。非常有用。