123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

var jsonObj = { city: "Chicago", hobbies: {work: ["coding", "leadership", "coffee making"], play: "biking"}, married: "false"}

console.log(jsonObj)

document.write(jsonObj.city);
document.write("<br>");
document.write(jsonObj.hobbies.work);
document.write("<br>");
document.write(jsonObj.hobbies.work[1]);
document.write("<br>");
document.write(jsonObj.hobbies.play);
document.write("<br>");
document.write(jsonObj.married);

document.write("<br>");
document.write(JSON.stringify(jsonObj, null, 4)); // Indented 4 spaces
document.write("<br>");
document.write(JSON.stringify(jsonObj, null, "\t")); // Indented with tab
document.write("<br>");
document.write(JSON.stringify(jsonObj.city, null, 4)); // Indented 4 spaces
document.write("<br>");
document.write(JSON.stringify(jsonObj.city, null, "\t")); // Indented with tab


function changingJson() {
  
 // jsonObj.city.push("goop");
  document.write("<hr>");
document.write(JSON.stringify(jsonObj, null, 4)); // Indented 4 spaces
  jsonObj.city = "Windy" ; //works!
  document.write("<hr>");
document.write(JSON.stringify(jsonObj, null, 4)); // Indented 4 spaces
  delete jsonObj["hobbies"]["play"]; //works!
  document.write("<hr>");
document.write(JSON.stringify(jsonObj, null, 4)); // Indented 4 spaces
  jsonObj.hobbies.work[0];
  document.write("<hr>");
  document.write(JSON.stringify(jsonObj.hobbies.work[2], null, 4));
  document.write("<br>");
  jsonObj.newValue = 'supposedToBeANewValueAdded'; //completely new value added to the jsObj
  document.write("<br>");
  document.write(JSON.stringify(jsonObj, null, 4));
  document.write("<br>");
  document.write(jsonObj["hobbies"]["work"]);
  document.write("<br>");
  document.write("<hr>");
  jsonObj.friends = ["john", "paul", "luke"];
  document.write(JSON.stringify(jsonObj, null, 4));
  document.write("<br>");
  document.write("<hr>");
  var variableToAddAsElement = [];
  variableToAddAsElement.push("love");
  variableToAddAsElement.push("war");
  variableToAddAsElement.push("hate");
  variableToAddAsElement.push("peace");
  jsonObj.pushedList = variableToAddAsElement;
  document.write(JSON.stringify(jsonObj, null, 4));
  document.write("<br>");
  document.write("<hr>");
  
  document.write(JSON.stringify(jsonObj, null, 4));
  
 
  
  
 return jsonObj; 
}

changingJson();

function showProps(obj, objName) {
  var result = "";
  for (var i in obj) {
    if (obj.hasOwnProperty(i)) {
      result += objName + "." + i + " = " + obj[i] + "\n";
    }
  }
  document.write("<p>object properties results</p>")
  document.write(result);
  document.write("<hr>")
  return result;
}

//wont show the inner part of hobbies (hobbies: {work: {3}, play: 1})

showProps(jsonObj, "CreatedBY:");

function Sex(top, bottom, gender, sexuality) {
  this.top = top;
  this.bottom = bottom;
  this.gender = gender;
  this.sexuality = sexuality;
}  
var mySexFriendTest = new Sex("yup", "yup", "queer", "pan");
var mySexFriendTest2 = new Sex("no", "no", "Female", "ace");

function Friend(name, sex, age, sexFriend) {
  this.name = name;
  this.sex = sex;
  this.age = age;
  this.sexFriend = sexFriend;
}  
var myFriendTest = new Friend("Test", "M", 25, mySexFriendTest);
var myFriendTest2 = new Friend("Test2", "F", 31, mySexFriendTest2);

document.write(myFriendTest);
document.write(myFriendTest2);
document.write("<br>");
document.write("<hr>");
document.write(JSON.stringify(myFriendTest, null, 4));
document.write("<hr>");
document.write(JSON.stringify(myFriendTest2, null, 4));
console.log(myFriendTest)
console.log(myFriendTest2)
mySexFriendTest.secret = "definitely"; //why did it add to the object in a random location? nm, I think the consol it setting it to be alphabetically


/*
You can add a property to a previously defined object type by using the prototype property. This defines a property that is shared by all objects of the specified type, rather than by just one instance of the object. The following code adds a color property to all objects of type car, and then assigns a value to the color property of the object car1.

// Car.prototype.color = null;
// car1.color = "black";
*/



            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console