简体   繁体   English

为什么不能使用原型将属性绑定到对象?

[英]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.

相关问题 为什么我不能设置使用'bind'创建的函数的'prototype'? - Why can't I set the 'prototype' of a function created using 'bind'? 为什么在不使用原型,调用或应用的情况下不能向对象添加属性? - Why can't I add a property to an object without using prototype, call or apply? 为什么当我在此JavaScript对象上使用反射时,看不到其原型对象中定义的属性? - Why when I use reflection on this JavaScript object I can't see a property defined in its prototype object? 为什么对象原型中的属性不能被对象修改? - why property in prototype of an object can not modified by object? 为什么不能通过其原型访问类中“ this”上的属性? - Why can’t I access a property on “this” in a class via its prototype? 为什么构造函数不能访问他原型的属性,但可以访问父原型的属性(原型的原型) - Why constructor function can't access property of his prototype, but can access property of parent prototype (prototype of prototype) 为什么我不能将函数分配给Object.prototype.click? - Why can't I assign a function to Object.prototype.click? 为什么不能使用原型将函数分配给变量? - Why can't I assign a function to a variable using prototype? 为什么JavaScript中的对象不能成为它自己的原型? - Why can't an object in JavaScript be a prototype of itself? 为什么不能设置此对象属性? - Why can't I set this object property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM