簡體   English   中英

為什么不能使用原型將屬性綁定到對象?

[英]Why can't I bind a property to an object using the prototype?

我有劇本...

 var MyClass = {
        someText: 'Hello World'
    };


    jQuery(document).ready(function () {

        console.log(MyClass.someText);
        MyClass.prototype.someText = 'proto Hello World';
        console.log(MyClass.someText);

    });

但我有一個例外說...

Microsoft JScript runtime error: `MyClass.prototype is null or not an object`

但是,當我閱讀有關原型的內容時,它說它可用於對象的所有實例,但以上內容似乎證明了這一點。 Whagwan?

為什么我的JavaScript不起作用?

因為您不了解原型是如何工作的。 函數定義原型,對象繼承。

var MyClass = function () { };
MyClass.someText = 'Hello World';
MyClass.prototype.someText = 'proto Hello World';

要獲得繼承原型屬性或方法的對象,您需要創建一個實例:

var myInstance = new MyClass();
console.log(myInstance.someText);

您還可以使用ES 5方法Object.create()創建具有特定內部[[Prototype]]的對象:

var myObj = Object.create({someText: 'proto Hello World'});
console.log(myObj.someText);

如果您像這樣創建MyClass對象:

var MyClass = function() {};

然后自動創建原型。

嘗試這個

var MyClass = {someText:'Hello World'};

jQuery(document).ready(function(){

        console.log(MyClass.someText);
        MyClass.someText = 'proto Hello World';
        console.log(MyClass.someText);

});

要么

   var MyClass = function () { };
   MyClass.someText = 'Hello World';
   MyClass.prototype.someText = 'proto Hello World';

在這種情況下,您不需要.prototype:

var MyClass = {     someText: 'Hello World' }; 
alert (MyClass.someText);
MyClass.someText = 'proto Hello World';
alert (MyClass.someText);

會提醒“ Hello World”和“ proto Hello World”

編輯:我想跟進一些有用的信息-並不是真正的超級新手,但這也許會幫助某人成功。

某些背景: http : //ejohn.org/blog/simple-class-instantiation/

工作示例: 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM