JSFiddle - React, Tailwind, and code Playground
by eitanp461
JavaScript
// An object we might use as a prototype
var john = {
name: "john",
gender: "male",
age: 47
};
// A constructor function, note the capital P
var Person = function (name, gender, age) {
this.name = name;
this.gender = gender;
this.age = age;
};
// Using `Person` (the constructor function)
var mary = new Person("Mary", "female", 32);
function isPlainObject(val) {
return val ? val.constructor === {}.constructor : false;
}
console.log('john.isPlainObject ' + isPlainObject(john)); // true
console.log('mary.isPlainObject ' + isPlainObject(mary)); // false
function countScopeObjects(scope, isLiteral) {
var objectCount = 0;
Object.keys(scope).filter(function (element) {
// Skip Angular's internal scope properties
return element.indexOf('$') === -1
}).forEach(function (propName) {
var isLiteralObject = isPlainObject(scope[propName]);
if (isLiteral === isLiteralObject) {
objectCount++;
}
// console.log(propName + ' -- ' + (isLiteral ? isLiteralObject : !isLiteralObject));
});
return objectCount; // Can implement with map reduce too
}
function countScopeLiteralObjects(scope) {
return countScopeObjects(scope, true);
}
function countScopeNonliteral(scope) {
return countScopeObjects(scope, false);
}
// Tweaking https://github.com/angular/angular.js/blob/master/src/ngMock/angular-mocks.js#L2163
function countLiterals() {
var rootScope = angular.element(document.body).injector().get('$rootScope');
// jshint validthis: true
var countLiterals = countScopeLiteralObjects(rootScope),
countNonliterals = countScopeNonliteral(rootScope);
var pendingChildHeads = [rootScope.$$childHead];
var currentScope;
while (pendingChildHeads.length) {
currentScope = pendingChildHeads.shift();
while (currentScope) {
countLiterals += countScopeLiteralObjects(currentScope);
countNonliterals +=...