Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this JS code is not valid?

Tags:

javascript

Below code snipped throws an error TypeError: myObj.prototype is undefined. Could someone explain me why?

Why there is no prototype for new Object() & object literals as specified below?

var myObj = {
    a : "This is a",
    b : "This is b"
}

myObj.prototype.c= "This is c";  // TypeError: myObj.prototype is undefined

If this is not valid approach then how can I achieve this?

like image 572
fe-ninja Avatar asked May 24 '13 12:05

fe-ninja


People also ask

Which is not valid in JavaScript?

For example, break or boolean variable names are not valid. JavaScript variable names should not start with a numeral (0-9). They must begin with a letter or an underscore character. For example, 123test is an invalid variable name but _123test is a valid one.

Why is my JavaScript code not working in Chrome?

Google Chrome In the "Settings" section click on the "Show advanced settings..." Under the the "Privacy" click on the "Content settings...". When the dialog window opens, look for the "JavaScript" section and select "Allow all sites to run JavaScript (recommended)". Click on the "OK" button to close it.

Why JS file is not working in HTML?

“js file not loading in html” Code Answer Most likely, the problem is that you are including your js file in a head tag or somewhere above your main content. ... You should be able to add the js file in a script tag. The page loads first then JavaScript.

What is the problem with JavaScript?

These days, most cross-browser JavaScript problems are seen: When poor-quality browser-sniffing code, feature-detection code, and vendor prefix usage block browsers from running code they could otherwise use just fine. When developers make use of new/nascent JavaScript features, modern Web APIs, etc.)


1 Answers

In earlier versions of EcmaScript you could not directly access the prototype of objects; the prototype property existed only on functions, and it comes into play when they are used as constructors. So you could do this:

// This is the myObj constuctor
function myObj() {
    this.a = "This is a";
    this.b = "This is b";
}

// Setting a property on the constructor prototype
// All instances will share this
myObj.prototype.c= "This is c";

// Creating a new object and testing its "c" property
var obj = new myObj();
alert(obj.c); // "This is c"

Modern browsers implement Object.getPrototypeOf, which means you can do this:

var myObj = {
    a : "This is a",
    b : "This is b"
}

Object.getPrototypeOf(myObj).c= "This is c";

However, you have to be careful! If you do this, then all objects that exist now and all objects that get created in the future will inherit the property c through their prototype chain!

This is because myObj is of type Object, and the prototype of Object is inherited by everything that is an object of any type. This leads to:

var myObj = {
    a : "This is a",
    b : "This is b"
}

Object.getPrototypeOf(myObj).c= "This is c";

var anotherObject = {};

alert(anotherObject.c); // "This is c" -- was it expected?

See it in action.

like image 157
Jon Avatar answered Oct 05 '22 12:10

Jon