[英]Why can't I bind a property to an object using the prototype?
I have the script... 我有剧本...
var MyClass = {
someText: 'Hello World'
};
jQuery(document).ready(function () {
console.log(MyClass.someText);
MyClass.prototype.someText = 'proto Hello World';
console.log(MyClass.someText);
});
But I get an exception saying... 但我有一个例外说...
Microsoft JScript runtime error: `MyClass.prototype is null or not an object`
But when I read about the prototype it says its avaiable to all instances of an object but the above seems to disprove this. 但是,当我阅读有关原型的内容时,它说它可用于对象的所有实例,但以上内容似乎证明了这一点。 Whagwan?
Whagwan?
Why does my javascript not work?
为什么我的JavaScript不起作用?
Because you do not understand how prototypes work. 因为您不了解原型是如何工作的。 Functions define prototypes, objects inherit.
函数定义原型,对象继承。
var MyClass = function () { };
MyClass.someText = 'Hello World';
MyClass.prototype.someText = 'proto Hello World';
To get an object that inherits a prototype property or method, you need to create an instance: 要获得继承原型属性或方法的对象,您需要创建一个实例:
var myInstance = new MyClass();
console.log(myInstance.someText);
You can also create an object with a particular internal [[Prototype]] by using the ES 5 method Object.create()
: 您还可以使用ES 5方法
Object.create()
创建具有特定内部[[Prototype]]的对象:
var myObj = Object.create({someText: 'proto Hello World'});
console.log(myObj.someText);
If you create the MyClass object like this: 如果您像这样创建MyClass对象:
var MyClass = function() {};
Then the prototype is automatically created. 然后自动创建原型。
Try this 尝试这个
var MyClass = { someText: 'Hello World' }; var MyClass = {someText:'Hello World'};
jQuery(document).ready(function () { jQuery(document).ready(function(){
console.log(MyClass.someText);
MyClass.someText = 'proto Hello World';
console.log(MyClass.someText);
});
or 要么
var MyClass = function () { };
MyClass.someText = 'Hello World';
MyClass.prototype.someText = 'proto Hello World';
You do not need the .prototype in this instance: 在这种情况下,您不需要.prototype:
var MyClass = { someText: 'Hello World' };
alert (MyClass.someText);
MyClass.someText = 'proto Hello World';
alert (MyClass.someText);
will alert "Hello World" and "proto Hello World" 会提醒“ Hello World”和“ proto Hello World”
EDIT: I wanted to follow up with some useful information - not really super new but perhaps this will help someone along the way to success. 编辑:我想跟进一些有用的信息-并不是真正的超级新手,但这也许会帮助某人成功。
SOME BACKGROUND: http://ejohn.org/blog/simple-class-instantiation/ 某些背景: http : //ejohn.org/blog/simple-class-instantiation/
Working example: http://jsfiddle.net/MarkSchultheiss/4GZha/#base 工作示例: http : //jsfiddle.net/MarkSchultheiss/4GZha/#base
// makeClass - By John Resig (MIT Licensed)
function makeClass() {
return function(args) {
if (this instanceof arguments.callee) {
if (typeof this.init == "function") this.init.apply(this, args.callee ? args : arguments);
} else return new arguments.callee(arguments);
};
}
var MyClass = makeClass();
MyClass.prototype.init = function(newText, newThing) {
this.someText = newText == undefined ? 'defaultText' : newText;
this.newThing = newThing == undefined ? 'defaultThing' : newThing ;
;
this.gobble = 'turkey';
this.cracker = 'cheese';
}
MyClass.prototype.otherText = 'otherstuff';
var trustme = MyClass('trustmedude');
alert(trustme.someText +":"+ trustme.newThing);
var iam = MyClass('some new text', 'mything');
alert("tmething "+trustme.newThing);
alert(iam.someText); //returns "some new text"
iam.someText = 'hi fred';
alert(iam.someText); //returns "some ne text"
alert(iam.otherText); // returns "otherstuff"
alert(iam.newThing); //returns "mything"
alert(MyClass.someText); //returns undefined
alert(MyClass.otherText); //returns undefined
alert(iam.cracker + iam.gobble); //returns "cheeseturkey"
alert(iam.hasOwnProperty("someText")); //returns true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.