-3

I have written a javascript prototype function, in which i need to get value by passing variable like below

myarr.value(1,2)="Content";

myarr.prototype.value = function (rows,columns){
        console.log(rows,columns);
}

In the above function I'm getting rows and columns as 1 & 2, but getting error msg "invalid assignment left-hand side" for the content.

Help me to get the content in this way? and mention what am i doing wrong here?

11
  • 2
    Well, yes, you cannot assign to a function call. It's unclear what you expect the result to be… Commented Sep 6, 2016 at 9:04
  • undefined = "Content". That's what your code does (assuming the first line is invoked on an object instance). There is no way this could work. Commented Sep 6, 2016 at 9:04
  • What is myarr ? A constructor ? Commented Sep 6, 2016 at 9:05
  • what myarr.value(1,2)="Content"; you think should do? Commented Sep 6, 2016 at 9:06
  • I 'm trying to assign array value of [1,2] as "Content", i'm unclear of getting the variable. Commented Sep 6, 2016 at 9:06

1 Answer 1

0

What is myarr ? Is it a kind of matrix ? How did you create it ?

From what I understand, you have a matrix and you want to set values using the coordinates... If you want to use the prototype like that it will not work... You have to create a Matrix object and add the value method to its prototype. Something like that

var Matrix = function(/*Some initializers*/){
  // Initialize the matrix
}

Matrix.prototype.setValue = function(x, y, value){
  // return something
}

// Then you use it like that
var mymatrix = new Matrix();

myMatrix.setValue(2, 12, 'someValue');

But what it is important is that you have to define how you build your matrix and how you access the elements... Hope it helps

Sign up to request clarification or add additional context in comments.

2 Comments

This is what i'm doing, but dont want to get content in this way like passing parameters, i need to get the content like "myMatrix.setValue(2,12) = "SomeValue"
You "need to" ? What are you trying to achieve ? What's the problem doing it like that ? You will not be able to do it like that except if you do something like this : myMatrix[2][12] = 'someValue'... But it means you dont use the prototype at all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.