• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Merchant Labs

Helping online merchants succeed is what we do best!

  • About
  • Contact
  • My Account

Experiments / February 14, 2017

Javascript Scope Experiments

I wanted to play around with wrapping my head around exactly how much wiggle room there is in Javascript scopes

var AC = function() {
function pigs_go () {
return “oink”;
}
return this;
}
pigs_go(); // doesnt work since this function doesnt exist in this scope
AC.pigs_go(); // this doesnt work either since pigs_go is a private method

Here is another test:

var AC = function(options){ // creating a class that creates an object with properties and methods as well as private variables to use.
if (typeof options != ‘object’) options = {};
var one = options.one ? options.one: 1; // private var
this.two = options.two ? options.two :2; // public property
return {
‘me’: this, // this property provides access to the entire class vars except private vars
‘work’: function() {
return this.two;
},
‘add’: function(num) {
return num + one;
}
};
};
var ac = new AC({one: 100, two: 200});
console.log(ac); // ac has ac.me (me only has me.two), ac.work(), and ac.add(num)

Filed Under: Experiments Tagged With: Coding, Javascript

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Footer

Shopify Partner

Let Merchant Labs, with over 15 years experience in ecommerce, help you SUCCEED online! From …

Read More about Shopify Development Services

  • Twitter
  • YouTube

Ajax-Cart Coming Soon

What is Ajax-cart for Shopify? Ajax-cart for Shopify is a big project to bring some of the most wanted features to the Shopify cart page. This page is also known as the basket screen. Its …

more about Ajax-Cart for Shopify

Tags

Apps Script Coding Documentation Javascript MySQL PHP Shopify Liquid

Contact Us

Merchant Labs
Los Angeles, California

P: 424-236-0372
E: [email protected]

Proud Partners For

Copyright © 2025 · Digital Pro on Genesis Framework · WordPress · Log in