简体   繁体   English

对象的价值和原型问题

[英]Object's value and prototype issue

I'm trying to create a new class, but it doesn't seem to be working. 我正在尝试创建一个新类,但是它似乎没有用。 Here's the code: 这是代码:

<script>
var myClass = function(arg){
    return new init(arg);
}
var init = function(arg){
    return arg;
}
init.prototype.prop = true;
</script>

The problem is that myClass('foo') doesn't return 'foo' as expected. 问题是myClass('foo')没有按预期返回'foo' Instead, it returns init . 而是返回init Then I tried this code: 然后我尝试了这段代码:

var init = function(arg){
    return [arg];//Now init returns an array
}

The myClass('foo') returns ['foo'] , but then myClass('foo').prop turns into undefined . myClass('foo')返回['foo'] ,但是myClass('foo').prop变为undefined Does anyone know how to solve it? 有人知道如何解决吗?

EDIT: I want to create this class more or less as jQuery does. 编辑:我想像jQuery一样或多或少创建此类。 For example: $('div') returns directly a value (in case, all div tags), but not an object with a property that stores this values 例如: $('div')直接返回一个值(以防所有div标签),但不返回具有存储该值的属性的对象

When you call new init() and within init() you return a non-object, it yields an instance of init instead of the returned value. 当您调用new init()且在init()您返回一个非对象,它将产生一个init实例,而不是返回值。

If you return an Array from your init() function, it gets returned instead; 如果您从init()函数返回一个Array,则返回它。 but Array doesn't take your init prototype into consideration :) 但是Array并没有考虑您的init原型:)

See also: https://stackoverflow.com/a/1978474/1338292 另请参阅: https : //stackoverflow.com/a/1978474/1338292

If you want to be like an array, you can do this: 如果要像数组一样,可以执行以下操作:

function myClass(arg) {
    return new init(arg);
}
function init(arg) {
    this.push(arg);
}
init.prototype = [];
init.prototype.foo = true;

var x = new init('foo');
console.log(x) // ["foo"]
console.log(x.foo) // true

It makes init inherit from the Array prototype (ie [] ). 它使initArray原型(即[] )继承。 After that you can add more properties and methods to the init prototype as you wish. 之后,您可以根据需要向init原型添加更多属性和方法。

In you code: 在您的代码中:

> <script>
> var myClass = function(arg) {
>   return new init(arg);
> }
> var init = function(arg){
>   return arg;
> }
>
> init.prototype.prop = true;
> </script>
>

The problem is that myClass('foo') doesn't return 'foo' as expected. 问题是myClass('foo')没有按预期返回'foo'。 Instead, it returns init. 而是返回init。 Then I tried this code: 然后我尝试了这段代码:

Your code may be clearer if written as function declarations (which is more common): 如果编写为函数声明(更常见),则代码可能会更清晰:

function myClass(arg) {
  return new init(arg);
}

function init(arg) {
  return arg;
}

When you call myClass('foo') , you pass a string to init , which is called as a constructor. 当调用myClass('foo') ,您将一个字符串传递给init ,它被称为构造函数。 The function then attempts to return the string, however a function called as a constructor will always return an object. 然后,该函数尝试返回字符串,但是称为构造函数的函数将始终返回对象。 If you try to return a primitive, it will instead return its this object. 如果您尝试返回一个原语,它将返回它的对象。 It's not very clearly stated in ECMA-262 . ECMA-262中没有明确说明。 but it is there. 但它在那里。

Then I tried this code: 然后我尝试了这段代码:

var init = function(arg) { return [arg]; var init = function(arg){return [arg]; //Now init returns an array //现在init返回一个数组

Because an array is an object, so that is what is returned. 因为数组是一个对象,所以返回的就是它。

> }
> 

The myClass('foo') returns ['foo'], but then myClass('foo').prop turns into undefined. myClass('foo')返回['foo'],但是myClass('foo')。prop变为未定义。 Does anyone know how to solve it? 有人知道如何解决吗?

Because your constructor returns an array, not its this object. 因为您的构造函数返回一个数组,而不是其this对象。 The returned array inherits from Array.prototype , not init.prototype . 返回的数组继承自Array.prototype ,而不是init.prototype

If you return a value from a function and try to use the new keyword with it, the return value will not be a newly instantiated object, but rather the value following the return . 如果从函数返回一个值并尝试使用new关键字,则返回值将不是新实例化的对象,而是return后面的值。 So in your case the 'foo' string is being returned, not a new object as you expect. 因此,在您的情况下,将返回“ foo”字符串,而不是您期望的新对象。

If you want to create a new class, you might want to consider this pattern: 如果要创建一个新类,则可能需要考虑以下模式:

var MyClass =function(arg) {
    this.init(arg);
};
MyClass.prototype = {
    init: function(arg) {
        // Do something with that arg
        this.someArg = arg;
    },
    myProperty: 'Hell world',
    someArg: null   
};

var myVar = new MyClass('Some arg');

Test is out: 测试出来了:

console.log(myVar.myProperty);
console.log(myVar.someArg);

Check it out: http://jsfiddle.net/4zwpm/ 检查一下: http : //jsfiddle.net/4zwpm/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM